From: Antony on
Didier Verna wrote:
> Hi,
>
> the standard defines two generic functions: slot-missing and
> slot-unbound. However, slot-unbound triggers an unbound-slot error by
> default, whereas slot-missing only triggers an error. So why the
> difference ?
>
I have a related question
In the slot-unbound method am I allowed to set the value of the slot.
It wasn't clear to me from the CLHS whether the obj is allowed to be
modified.
My thought was that I can use this to optimize computation of expensive
slot values.

-Antony

From: Pascal Costanza on
On 12/01/2010 18:29, Antony wrote:
> Didier Verna wrote:
>> Hi,
>>
>> the standard defines two generic functions: slot-missing and
>> slot-unbound. However, slot-unbound triggers an unbound-slot error by
>> default, whereas slot-missing only triggers an error. So why the
>> difference ?
>>
> I have a related question
> In the slot-unbound method am I allowed to set the value of the slot.
> It wasn't clear to me from the CLHS whether the obj is allowed to be
> modified.
> My thought was that I can use this to optimize computation of expensive
> slot values.

Yes, that's actually a good idea.


Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: Tamas K Papp on
On Tue, 12 Jan 2010 09:29:25 -0800, Antony wrote:

> Didier Verna wrote:
>> Hi,
>>
>> the standard defines two generic functions: slot-missing and
>> slot-unbound. However, slot-unbound triggers an unbound-slot error by
>> default, whereas slot-missing only triggers an error. So why the
>> difference ?
>>
> I have a related question
> In the slot-unbound method am I allowed to set the value of the slot. It
> wasn't clear to me from the CLHS whether the obj is allowed to be
> modified.
> My thought was that I can use this to optimize computation of expensive
> slot values.

It is a good idea. I use this macro for "lazy" calculation of
expensively computed slots:

(defmacro define-unbound-slot-initializer ((class slot &optional
(instance 'instance))
form)
"Define a method for SLOT-UNBOUND that initializes the given slot with
FORM, binding the instance to INSTANCE."
(assert (and (symbolp slot) (symbolp instance)))
`(defmethod slot-unbound (class (,instance ,class)
(slot (eql ',slot)))
(setf (slot-value ,instance ',slot) ,form)))

Tamas
From: Duane Rettig on
On Jan 12, 7:40 pm, Rahul Jain <rj...(a)nyct.net> wrote:
> Duane Rettig <du...(a)franz.com> writes:
> > Slot-unbound is simply a data error: you've failed to supply some data
> > for your instance.  Slot-missing is a category error; you are trying
> > to describe structure which you have not defined in your class.
>
> Unless you're me, writing my prototypes library. :(

Assuming you're using the MOP: whatever you did to get a slot-missing
error you could have avoided by checking for the presence of the slot
via:

(find slot-name (mop:class-slots class) :test #'eq :key #'mop:slot-
definition-name)

Even without the MOP you are still not without recourse: since slot-
missing is a generic-function, you can define a method via a subclass
or mixin which defines any different behavior, thus avoiding the
error.

Duane
From: Thomas A. Russ on
Tamas K Papp <tkpapp(a)gmail.com> writes:

> On Tue, 12 Jan 2010 09:29:25 -0800, Antony wrote:
....
> > In the slot-unbound method am I allowed to set the value of the slot. It
> > wasn't clear to me from the CLHS whether the obj is allowed to be
> > modified.
> > My thought was that I can use this to optimize computation of expensive
> > slot values.
>
> It is a good idea. I use this macro for "lazy" calculation of
> expensively computed slots:

And we have used the SLOT-MISSING method to implement "dynamic slots" on
classes. If a slot is missing, then it becomes a dynamic slot that is
stored in an alist or hash-table with its value. That allows regular
slots to be fast, and the much less common dynamic slots to be
implemented on an as-needed basis. This was also done as a technique to
save space for seldom used slots.

--
Thomas A. Russ, USC/Information Sciences Institute