3

我正在尝试在 Common Lisp 中对多方法进行“重载调用”。以下是案例的简化概要:

(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*)

输出:i pity the foo defnoodle, who has a bar Chocolate

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

 (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]

有谁知道我在这里做错了什么?我知道初始化实例具有类似的灵活性,因为应该能够识别每个类和任意数量的参数的 n 个初始化实例方法。

(defmethod initialize-instance :after ((f foo) &key)
  ())

但我不清楚如何将其转换为我上面给出的香草示例。而且我觉得我可能会叫错树,因为这是 MOP 的一部分。

4

2 回答 2

7

你写道:有谁知道我在这里做错了什么?

说清楚:CLOS 不支持。在单个泛型函数的方法的方法参数列表中,您不能有不同数量的必需参数。调度仅对所需的参数起作用。Common Lisp 不支持“重载”。

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

initialize-instance instance &rest initargs &key &allow-other-keys => instance

所有方法都采用一个必需的参数,即实例。仅在此对象上进行调度。然后它允许各种关键字参数 - 不为它们进行分派。

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

有关规则,请参阅 CL Hyperspec:Congruent Lambda-lists for all Methods of a Generic Function

于 2014-02-20T07:47:00.050 回答
4

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

但是,当然,您不能专门化&optional,&rest&key参数的类型。

于 2014-02-20T00:44:16.813 回答