Search code examples
common-lispclos

Weird error when compiling Common Lisp code


I am getting the following error trying to compile some code:

Lambda list of method # is incompatible with that of the generic function INITIALIZE-INSTANCE. Method's lambda-list : (PAT::E) Generic-function's : (CCL::INSTANCE &REST CCL::INITARGS &KEY &ALLOW-OTHER-KEYS)

Here is the code causing the error:

(defclass event ()
  ((timestamp
    :initarg :timestamp
    :accessor timestamp)
   (value
    :initarg :value
    :accessor value)))

(defclass update (event)
  ((security
    :initarg :sectype
    :accessor sectype)))

(defclass prc (update)
  ((lastp
    :accessor lastp)
   (lastv
    :accessor lastv)))

(defmethod initialize-instance :after ((e prc)) ; <- :(
  (setf (lastp e) (first (value e)))
  (when (second (value e))
    (setf (lastv e) (second (value e)))))

Any hints as to what might be causing the error would be really appreciated.


Solution

  • You need to add &key at the end of the argument list to your initialize-instance method.

    To quote from "Practical Common Lisp", chapter "17. Object Reorientation: Classes":

    The &key in the parameter list is required to keep the method's parameter list congruent with the generic function's--the parameter list specified for the INITIALIZE-INSTANCE generic function includes &key in order to allow individual methods to supply their own keyword parameters but doesn't require any particular ones. Thus, every method must specify &key even if it doesn't specify any &key parameters.