On Tue, 20 Apr 2004 14:11:06 -0600 (MDT), Jim Prewett <[EMAIL PROTECTED]> wrote:

> (defclass counted-class ()
>       ((counter :initform 0)))
>
> (defmethod make-instance :after ((counted-class counted-class)) &rest args)
>       (incf (slot-value counted-class 'counter)))

There should only be one closing paren after COUNTED-CLASS.

> the instances that are returned always have 0 in the counter slot.
>
> any ideas?

MAKE-INSTANCE dispatches on the class not on the object, i.e. to have
it called when a COUNTED-CLASS object is created you want something
like

  (defmethod make-instance :after
      ((counted-class (eql 'counted-class)) &rest args)

but what you actually want is an :AFTER method for
INITIALIZE-INSTANCE:

  * (defclass counted-class ()
      ((counter :initform 0)))

  #<STANDARD-CLASS COUNTED-CLASS {486666CD}>
  * (defmethod initialize-instance :after
        ((object counted-class) &rest init-args)
      (declare (ignore init-args))
      (incf (slot-value object 'counter)))

  #<Standard-Method INITIALIZE-INSTANCE :AFTER (COUNTED-CLASS) {4866AA8D}>
  * (describe (make-instance 'counted-class))


  #<COUNTED-CLASS {4866D05D}> is an instance of class #<Standard-Class COUNTED-CLASS
                                                        {486627D5}>:
   The following slots have :INSTANCE allocation:
   COUNTER    1

Cheers,
Edi.


Reply via email to