调用下一个最具体的方法不起作用

时间:2017-06-18 09:25:25

标签: methods parameters common-lisp clos

考虑班级account

(defclass account ()
  ((name :initarg :name :reader name)
   (balance :initarg :balance :initform 0.00 :accessor balance)
   (interest-rate :allocation :class :initform 0.06
                  :reader interest-rate)))

对于这个类,我们定义了一个方法withdraw

(defmethod withdraw ((acct account) amt)
  (if (< amt (balance acct))
      (decf (balance acct) amt)
      'insufficient-funds))

另一个类password-account,它是account的子类:

(defclass password-account (account)
  ((password :initarg :password :reader password )))

此方法的方法withdraw

(defmethod withdraw ((acct password-account) amt pass)
  (if (equal (password acct) pass)
      (call-next-method acct amt )
      'wrong-password))

但这会产生错误:

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

Restarts:
 0: [RETRY] Retry SLIME REPL evaluation request.
 1: [*ABORT] Return to SLIME's top level.
 2: [ABORT] abort thread (#<THREAD "repl-thread" RUNNING {1005308033}>)

为什么会这样?什么呢?

  

被要求找到一个带有特化器的方法

在这里?

这里,主withdraw函数有两个参数acctamt,所以为了从一个更具体的方法调用它,它使用3个参数而不是2个,我们可以向call-next-method提供不太具体的withdraw方法的参数。但这仍然不起作用 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:7)

通用函数的全等lambda列表

通用函数的方法需要congruent lambda列表。语言标准描述了这意味着什么:Congruent Lambda-lists for all Methods of a Generic Function

正如您所看到的,第一条规则是:

  • 每个lambda列表必须具有相同数量的必需参数。

必需参数告诉我们必须提供哪些参数始终。通用函数还允许可选,关键字和rest参数。但是这些都没有发送。调度仅适用于所需的参数和所有这些参数。

具有相同数量的必需参数使得调度更容易,并允许编译器检查具有错误数量的参数的函数调用。

可选参数也必须一致

另请注意,泛型函数的所有方法都需要具有相同数量的可选参数。请参阅标准中的第二条规则。

<强>措辞

  • 参数类似于lambda列表中的命名变量
  • 在函数调用中提供参数

示例:

(defun foo (a b) (list a b))
函数a

bfoo参数

(foo (+ 2 3) (* 4 5))

520是函数foo调用的两个参数