home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.lisp:3161 comp.lang.clos:1055
- Path: sparky!uunet!enterpoop.mit.edu!spool.mu.edu!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.lisp,comp.lang.clos
- Subject: Re: Automatic evaluation of expression in CLOS slots
- Followup-To: comp.lang.clos
- Date: 3 Jan 1993 19:52:36 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 40
- Message-ID: <1i7g64INN2tt@early-bird.think.com>
- References: <1993Jan3.023306.25733@cc.umontreal.ca>
- NNTP-Posting-Host: gandalf.think.com
-
- [I've redirected followups to comp.lang.clos, a more appropriate newsgroup.]
-
- In article <1993Jan3.023306.25733@cc.umontreal.ca> kardank@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.
-
- barmar@think.com {uunet,harvard}!think!barmar
-