I would like to be able to place expressions in class slots, and have them
transparently evaluated when accessed. Is there a standard way of doing
this? Do I need the Meta Object Protocol for this?
For example, I would like behaviour similar to the following:
(defclass foo ()
((slot-1 :accessor slot-1)))
(defvar x (make-instance 'foo))
(setf (slot-1 x) '(+ 2 2))
(slot-1 x) --> 4
(slot-value x 'slot-1) --> 4
(slot-expression x 'slot-1) --> '(+ 2 2)
Thanks,
Kaveh
--
------------
Kaveh Kardan ·····@taarna.UUCP
Systeme Taarna ·······@eole.umontreal.qc.ca
Montreal Quebec Canada (514)844-8448
[I've redirected followups to comp.lang.clos, a more appropriate newsgroup.]
In article <·····················@cc.umontreal.ca> ·······@ERE.UMontreal.CA (Kardan Kaveh) writes:
>I would like to be able to place expressions in class slots, and have them
>transparently evaluated when accessed. Is there a standard way of doing
>this? Do I need the Meta Object Protocol for this?
To get precisely what you described in your example, you need the MOP.
>(defclass foo ()
> ((slot-1 :accessor slot-1)))
>
>(defvar x (make-instance 'foo))
>
>(setf (slot-1 x) '(+ 2 2))
>
>(slot-1 x) --> 4
>(slot-value x 'slot-1) --> 4
>(slot-expression x 'slot-1) --> '(+ 2 2)
If you're willing to allow SLOT-VALUE to return the expression, but have
SLOT-1 perform the computation, you can do this without the MOP. You could
write:
(defclass foo ()
((slot-1 :writer (setf slot-1))))
(defmethod slot-1 ((self foo))
(eval (slot-value self 'slot-1)))
I think this should be sufficient, since functions outside the class should
not use SLOT-VALUE instead of the accessor function.
Naturally, all the usual warnings about using EVAL apply to this, no matter
how you arrange for the evaluation to be invoked.
--
Barry Margolin
System Manager, Thinking Machines Corp.
······@think.com {uunet,harvard}!think!barmar