Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lisp &引用;超载“;具有不同参数列表的CLOS多方法_Lisp_Common Lisp_Clos - Fatal编程技术网

Lisp &引用;超载“;具有不同参数列表的CLOS多方法

Lisp &引用;超载“;具有不同参数列表的CLOS多方法,lisp,common-lisp,clos,Lisp,Common Lisp,Clos,我试图在CommonLisp中对多方法进行“重载调用”。以下是该案例的简化摘要: (defclass foo () ((slotty :accessor slotty :initarg :slotty))) (defclass bar () ((slotty :accessor slotty :initarg :slotty))) (defparameter *foo* (make-instance 'foo :slotty "defnoodle")) (defparameter *

我试图在CommonLisp中对多方法进行“重载调用”。以下是该案例的简化摘要:

(defclass foo ()
  ((slotty :accessor slotty :initarg :slotty)))

(defclass bar ()
  ((slotty :accessor slotty :initarg :slotty)))

(defparameter *foo* (make-instance 'foo :slotty "defnoodle"))
(defparameter *bar* (make-instance 'bar :slotty "Chocolate"))

(defmethod contrived ((f foo) (b bar))
  (format t "i pity the foo ~A, who has a bar ~A ~%" (slotty f) (slotty b)))

(contrived *foo* *bar*)
输出:
我很同情foo defmoodle,他有一块巧克力

但一旦我尝试定义下一种方法:

 (defmethod contrived ((f foo))
    (format  t "i just pity the foo ~A ~%" (slotty f)))
CL发疯了:

; The generic function #<STANDARD-GENERIC-FUNCTION CONTRIVED (1)>
; takes 2 required arguments; was asked to find a method with
; specializers (#<STANDARD-CLASS FOO>)
;   [Condition of type SB-PCL::FIND-METHOD-LENGTH-MISMATCH]
; See also:
;  Common Lisp Hyperspec, FIND-METHOD [:function]

但我不清楚如何将其转化为我上面给出的普通示例。我觉得我可能找错人了,因为这是拖把的一部分

泛型函数的所有方法都必须有一个全等参数列表。如果具有相同数量的必需参数、相同数量的可选参数,并且以兼容的方式使用
&rest
&key
,则参数列表是一致的


但是,当然,您不能根据
&可选的
&rest
&键的
参数的类型进行专门化。

您写道:有人知道我在这里做错了什么吗

(defmethod initialize-instance :after ((f foo) &key)
  ())
明确地说:CLOS不支持这一点。在单个泛型函数的方法的方法参数列表中,不能有不同数量的必需参数。分派仅在所需参数上工作。Common Lisp不支持“重载”

INITIALIZE-INSTANCE
使用以下语法定义:

initialize-instance instance &rest initargs &key &allow-other-keys => instance
所有方法都接受一个必需的参数,即实例。仅在此对象上执行分派。然后,它允许各种关键字参数-不为它们执行调度

因此,您需要就泛型函数所需的参数数量达成一致,并在代码中以这种方式调用它


有关规则,请参阅CL Hyperspec:。

这不是重载。重载意味着早期(编译时)绑定相同名称的符号。CLOS所做的是分派,即延迟(运行时)绑定。如果有不同数量的参数,则无需将其推迟到运行时。约束条件是不能有多个相同完全限定名的泛型函数。