Lisp HUG Maillist Archive

AMOP and custom metaclasses

Dear list,

I would like to understand why the code in AMOP at chapter 3, page 72 does not work for me.  I have the feeling it has something to do with what is explained in the LispWorks manual at section 14.2.1 (Inheritance across metaclasses http://www.lispworks.com/documentation/lw60/LW/html/lw-224.htm).

Here is the code in question:

(defclass shape () ())
(defclass rectangle (shape) ())

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

(setf (find-class 'counted-rectangle)
      (make-instance 'counted-class
                     :name 'counted-rectangle
                     :direct-superclasses (list (find-class 'rectangle))
                     :direct-slots ()))


The last form fails with the error:

Error: #<STANDARD-CLASS RECTANGLE 219ECE1B> is an invalid superclass of #<COUNTED-CLASS COUNTED-RECTANGLE 2010504F>.


I tried to implement a specialization of validate-superclass, but that does not help:

(defmethod validate-superclass (class (superclass rectangle))
  (eq 'counted-rectangle (class-name class)))


I would appreciate if someone could help me understand this issue.
Have a nice week-end!


Camille

Re: AMOP and custom metaclasses

I have found the solution to my previous post, but would appreciate if someone could shed some details.


On 9 juil. 2011, at 18:38, Camille Troillard wrote:

I would like to understand why the code in AMOP at chapter 3, page 72 does not work for me.  I have the feeling it has something to do with what is explained in the LispWorks manual at section 14.2.1 (Inheritance across metaclasses http://www.lispworks.com/documentation/lw60/LW/html/lw-224.htm).

Here is the code in question:

(defclass shape () ())
(defclass rectangle (shape) ())

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

(setf (find-class 'counted-rectangle)
      (make-instance 'counted-class
                     :name 'counted-rectangle
                     :direct-superclasses (list (find-class 'rectangle))
                     :direct-slots ()))


The last form fails with the error:

Error: #<STANDARD-CLASS RECTANGLE 219ECE1B> is an invalid superclass of #<COUNTED-CLASS COUNTED-RECTANGLE 2010504F>.


I tried to implement a specialization of validate-superclass, but that does not help:

(defmethod validate-superclass (class (superclass rectangle))
  (eq 'counted-rectangle (class-name class)))

The correct implementation of validate-superclass is:

(defmethod validate-superclass ((class counted-class) (superclass standard-class))
  t)

I don't understand this since counted-class is already defined as a subclass of standard-class.


Cam

Updated at: 2020-12-10 08:37 UTC