编译 Common Lisp 代码时出现奇怪的错误
Weird error when compiling Common Lisp 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)
这是导致错误的代码:
(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)))))
任何关于可能导致错误的提示都将不胜感激。
您需要在 initialize-instance
方法的参数列表末尾添加 &key
。
引用自 "Practical Common Lisp",章节“17. 对象重定向:类”:
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.
我在尝试编译一些代码时遇到以下错误:
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)
这是导致错误的代码:
(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)))))
任何关于可能导致错误的提示都将不胜感激。
您需要在 initialize-instance
方法的参数列表末尾添加 &key
。
引用自 "Practical Common Lisp",章节“17. 对象重定向:类”:
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 theINITIALIZE-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.