:initform and slot-value 
Author Message
 :initform and slot-value

Consider the follwing:

(defclass bar ()
  ((a :initarg :a :initform 'a :accessor bar-a)
   (b :initarg :b :accessor bar-b)))

(defmethod initialize-instance :after ((b bar)
                                       &rest initargs
                                       &key &allow-other-keys)
  (unless (getf initargs :b)
    (setf (slot-value b 'b) (slot-value b 'a))))

My intention here was to use one slot value for initializing the other
if the :initarg for the latter wasn't been supplied to
make-instance. Is this above acceptable way for such things or better
one exists ?

--



Sat, 26 Jun 2004 23:48:40 GMT  
 :initform and slot-value
Not much diff, but FYI there's a language feature telling you if an
initarg was supplied:

(defmethod initialize-instance :after ((self bar)
                                        &rest initargs
                                        &key (b nil b-supplied)
                                        &allow-other-keys)
  (declare (ignore b))
   (unless b-supplied
     (setf (bar-b self) (bar-a self)))) ;; not sure why slot-value was
used

kenny
clinisys

Quote:

> Consider the follwing:

> (defclass bar ()
>   ((a :initarg :a :initform 'a :accessor bar-a)
>    (b :initarg :b :accessor bar-b)))

> (defmethod initialize-instance :after ((b bar)
>                                        &rest initargs
>                                        &key &allow-other-keys)
>   (unless (getf initargs :b)
>     (setf (slot-value b 'b) (slot-value b 'a))))

> My intention here was to use one slot value for initializing the other
> if the :initarg for the latter wasn't been supplied to
> make-instance. Is this above acceptable way for such things or better
> one exists ?

> --




Sun, 27 Jun 2004 00:39:37 GMT  
 :initform and slot-value
Yes, that way it seems more elegant. Of course no need to use
slots if accessors could.

Quote:

> Not much diff, but FYI there's a language feature telling you if an
> initarg was supplied:

> (defmethod initialize-instance :after ((self bar)
>                                         &rest initargs
>                                         &key (b nil b-supplied)
>                                         &allow-other-keys)
>   (declare (ignore b))
>    (unless b-supplied
>      (setf (bar-b self) (bar-a self)))) ;; not sure why slot-value was
> used

> kenny
> clinisys


> > Consider the follwing:

> > (defclass bar ()
> >   ((a :initarg :a :initform 'a :accessor bar-a)
> >    (b :initarg :b :accessor bar-b)))

> > (defmethod initialize-instance :after ((b bar)
> >                                        &rest initargs
> >                                        &key &allow-other-keys)
> >   (unless (getf initargs :b)
> >     (setf (slot-value b 'b) (slot-value b 'a))))

> > My intention here was to use one slot value for initializing the other
> > if the :initarg for the latter wasn't been supplied to
> > make-instance. Is this above acceptable way for such things or better
> > one exists ?

> > --


--



Sun, 27 Jun 2004 01:03:57 GMT  
 :initform and slot-value


Quote:
>Consider the follwing:

>(defclass bar ()
>  ((a :initarg :a :initform 'a :accessor bar-a)
>   (b :initarg :b :accessor bar-b)))

>(defmethod initialize-instance :after ((b bar)
>                                   &rest initargs
>                                   &key &allow-other-keys)
>  (unless (getf initargs :b)
>    (setf (slot-value b 'b) (slot-value b 'a))))

>My intention here was to use one slot value for initializing the other
>if the :initarg for the latter wasn't been supplied to
>make-instance. Is this above acceptable way for such things or better
>one exists ?

I recommend using SLOT-BOUNDP rather than checking for an initarg.  That
way, if a subclass provides some other way to initialize B, you won't
override it.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



Sun, 27 Jun 2004 01:36:47 GMT  
 :initform and slot-value

Quote:



> >My intention here was to use one slot value for initializing the other
> >if the :initarg for the latter wasn't been supplied to
> >make-instance. Is this above acceptable way for such things or better
> >one exists ?

> I recommend using SLOT-BOUNDP rather than checking for an initarg.  That
> way, if a subclass provides some other way to initialize B, you won't
> override it.

Good point, I hadn't thought of that.

I actually find the whole thing semantically obscure. I mean, you end up
documenting the slot b as working that way "if you do not supply b, b
will get the value in a". A subclass trying to go another way is
breaking that odd initialization contract.

IMO it is better to just let initialization initialize. Then if some
derived quality of the class involving b and a needs expression, simply:

   (defun c (self)
      (or (b self) (a self)))

kenny
clinisys



Sun, 27 Jun 2004 02:42:23 GMT  
 :initform and slot-value


Quote:




> > >My intention here was to use one slot value for initializing the other
> > >if the :initarg for the latter wasn't been supplied to
> > >make-instance. Is this above acceptable way for such things or better
> > >one exists ?

> > I recommend using SLOT-BOUNDP rather than checking for an initarg.  That
> > way, if a subclass provides some other way to initialize B, you won't
> > override it.

> Good point, I hadn't thought of that.

> I actually find the whole thing semantically obscure. I mean, you end up
> documenting the slot b as working that way "if you do not supply b, b
> will get the value in a". A subclass trying to go another way is
> breaking that odd initialization contract.

> IMO it is better to just let initialization initialize. Then if some
> derived quality of the class involving b and a needs expression, simply:

The below does not allow for nil being a possibly meaningful value...

Quote:
>    (defun c (self)
>       (or (b self) (a self)))

Or maybe better, leave it unbound at initialization and put a before method on
the b method:

(defmethod b :before ((obj foo))
    (when (slot-unboundp obj 'b)
         (setf (b obj) (a obj))))

Or if that side effect is unwanted, write your own getter:

(defmethod b ((obj foo))
    (if (slot-boundp obj 'b)
        (b obj)
        (a obj)))

--
Coby



Sun, 27 Jun 2004 04:14:12 GMT  
 :initform and slot-value
May I guess that SHARED-INITIALIZE would give more accurate
handling in all possible cases than INITIALIZE-INSTANCE could do ?

Quote:

> Consider the follwing:

> (defclass bar ()
>   ((a :initarg :a :initform 'a :accessor bar-a)
>    (b :initarg :b :accessor bar-b)))

> (defmethod initialize-instance :after ((b bar)
>                                        &rest initargs
>                                        &key &allow-other-keys)
>   (unless (getf initargs :b)
>     (setf (slot-value b 'b) (slot-value b 'a))))

> My intention here was to use one slot value for initializing the other
> if the :initarg for the latter wasn't been supplied to
> make-instance. Is this above acceptable way for such things or better
> one exists ?

> --


--



Sun, 27 Jun 2004 17:56:06 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. evaluation of defstruct slot initforms

2. New initforms for inherited slots vs simple-condition-format-control

3. Dylan Tip #101: don't use keyword init-values with class allocated slots

4. Limiting accepted values in slots

5. Objects with Lazy Evaluation Slot Values

6. :ALLOCATION :CLASS: getting slot value

7. incrementing slot value

8. Back door to 'slot-value-using-class?

9. Accessing foreign type array slot value (ACL 5.0)

10. with-slots and multiple-value-bind

11. Before method for SLOT-VALUE

12. What happens to return value of SLOT-UNBOUND?

 

 
Powered by phpBB® Forum Software