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

Assigning default slot value plus side effects?



I'm new to CLOS, so excuse me if this is a basic question.

I have a slot that, whenever a value is stored in it, I want
side effects to occur. For the sake of illustration lets say that
I just want to execute a format statement saying the slot has the
specified value. This, of course, is simple: I just write a :after
method on (setf <accessor-name>) for that slot.

Ie (defclass foo () ((bar :accessor bar)) )

   (defmethod (setf bar) :after (bar-value (object foo))
      (format t "~%Put the value ~S into bar slot of ~S." bar-value object))

My problem is with a default value for the slot. I want to assign a default
value, but still have my after method fire. Ie I added a :initform to
the bar slot of foo objects. However, initialize-instance
uses setf on slot-value, rather than using the accessor, so my :after
method does not fire when I make an instance of foo.

Certainly I could omit the :initform, do make-instance, then use
my accessor to set the value. But this seems to somewhat contradict the
object-oriented nature.

Have I overlooked an obvious approach? Is there some way to make an
after method on (setf (slot-value <foo-object> 'bar)) ?

Thanks for any suggestions-
				- Marty Hall
-------------------------------------------------------------------
hall@aplcen.apl.jhu.edu                 Artificial Intelligence Lab
hall%aplcen.apl.jhu.edu@cunyvm.bitnet   AAI Corp
..!uunet!aplcen!hall                    PO Box 126
(301) 683-6455                          Hunt Valley, MD 21030