home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / lisp / 3161 < prev    next >
Encoding:
Text File  |  1993-01-03  |  1.8 KB  |  54 lines

  1. Xref: sparky comp.lang.lisp:3161 comp.lang.clos:1055
  2. Path: sparky!uunet!enterpoop.mit.edu!spool.mu.edu!think.com!barmar
  3. From: barmar@think.com (Barry Margolin)
  4. Newsgroups: comp.lang.lisp,comp.lang.clos
  5. Subject: Re: Automatic evaluation of expression in CLOS slots
  6. Followup-To: comp.lang.clos
  7. Date: 3 Jan 1993 19:52:36 GMT
  8. Organization: Thinking Machines Corporation, Cambridge MA, USA
  9. Lines: 40
  10. Message-ID: <1i7g64INN2tt@early-bird.think.com>
  11. References: <1993Jan3.023306.25733@cc.umontreal.ca>
  12. NNTP-Posting-Host: gandalf.think.com
  13.  
  14. [I've redirected followups to comp.lang.clos, a more appropriate newsgroup.]
  15.  
  16. In article <1993Jan3.023306.25733@cc.umontreal.ca> kardank@ERE.UMontreal.CA (Kardan Kaveh) writes:
  17. >I would like to be able to place expressions in class slots, and have them
  18. >transparently evaluated when accessed.  Is there a standard way of doing
  19. >this?  Do I need the Meta Object Protocol for this?
  20.  
  21. To get precisely what you described in your example, you need the MOP.
  22.  
  23. >(defclass foo ()
  24. >  ((slot-1 :accessor slot-1)))
  25. >
  26. >(defvar x (make-instance 'foo))
  27. >
  28. >(setf (slot-1 x) '(+ 2 2))
  29. >
  30. >(slot-1 x)  -->  4
  31. >(slot-value x 'slot-1)  -->  4
  32. >(slot-expression x 'slot-1)  --> '(+ 2 2)
  33.  
  34. If you're willing to allow SLOT-VALUE to return the expression, but have
  35. SLOT-1 perform the computation, you can do this without the MOP.  You could
  36. write:
  37.  
  38. (defclass foo ()
  39.   ((slot-1 :writer (setf slot-1))))
  40.  
  41. (defmethod slot-1 ((self foo))
  42.   (eval (slot-value self 'slot-1)))
  43.  
  44. I think this should be sufficient, since functions outside the class should
  45. not use SLOT-VALUE instead of the accessor function.
  46.  
  47. Naturally, all the usual warnings about using EVAL apply to this, no matter
  48. how you arrange for the evaluation to be invoked.
  49. -- 
  50. Barry Margolin
  51. System Manager, Thinking Machines Corp.
  52.  
  53. barmar@think.com          {uunet,harvard}!think!barmar
  54.