[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

self-reflexive objects



>Date: Mon, 19 Oct 92 21:39:47 -0400
>From: gw@dsl.pitt.edu (gerhard werner)
>To: info-mcl@cambridge.apple.com
>Subject: self-reflexive objects
>
>My goal is to use objects in a self-reflexive way: that is, store methods 
>(functions) in slots which -if activated by an external signal- change a 
>value in another slot of the same object. I stumble over this and find no 
>clues in the literature available to me. Please advise.
>
>Thank you, Gerhard Werner (gwer1@unix.cis.pitt.edu)
>
>

How about this? The function stored in the REFLECTIVE-FUNCTION 
slot of *ME* is a function of one argument. 

Notice that it remembers its parent object through the lexical 
variable MYSELF. This trick is known in lisp circles as a "closure".

-------

(defclass solipsist ()
  ((reflective-function :accessor reflective-function)
   (bucket :accessor bucket :initform nil)))

(defmethod initialize-instance :after ((myself solipsist) &rest ignore)
  (declare (ignore ignore))
  (setf (reflective-function myself)
        #'(lambda (stuff)
            (format t "~%Putting ~A into (bucket ~A)" stuff myself)
            (setf (bucket myself) stuff))))


(defparameter *me* (make-instance 'solipsist))


? (describe *me*)
#<SOLIPSIST #x8BF0D1>
Class: #<STANDARD-CLASS SOLIPSIST>
Wrapper: #<CLASS-WRAPPER SOLIPSIST #x8BF0A1>
Instance slots
REFLECTIVE-FUNCTION: #<COMPILED-LEXICAL-CLOSURE #x8BF186>
BUCKET: NIL
? (funcall (reflective-function *me*) 'foo)

Putting FOO into (bucket #<SOLIPSIST #x8BF0D1>)
FOO
? (bucket *me*)
FOO
?