common-lispclos

How to use call-next-method in initialize-instance with multiple key arguments CLOS


If I have two classes, a class parent and a class child.

(defclass parent ()
    ...)

(defclass child (parent)
    ...)

And I've defined 2 different methods for initialize-instance, but the child one takes another argument, and calls call-next-method

(defmethod initialize-instance :after ((instance parent) &key)
    ...) ; Do things

(defmethod initialize-instance :after ((instance child) &key other-arg)
    ... ; Do things
    (call-next-method))

I get the error

There is no next method for the generic function
#<STANDARD-METHOD COMMON-LISP:INITIALIZE-INSTANCE (93)>
when called from method
#<STANDARD-METHOD COMMON-LISP:INITIALIZE-INSTANCE :AFTER (CHILD) {...}>
with arguments
 ....

Condition of type SB-PCL::NO-NEXT-METHOD-ERROR

Apparently, I can't call the next method with the supplied arguments?


Solution

  • In the standard method combination, which is what you are using, after methods don't have next methods, so call-next-method can't be used within them: rather applicable after methods are all called in most-specific-last order by the method combination. The places you are allowed to use call-next-method are primary and around methods.