【问题标题】:lisp clos accessor problemslisp clos 访问器问题
【发布时间】:2014-03-05 19:59:11
【问题描述】:

当类在列表中时,我不能使用 clos 访问器函数。

假设我有a类:

(defclass a ()
  ((a :accessor a
      :initarg :a)))

我做了 2 个实例:

(defparameter b (make-instance 'a :a 1))
(defparameter c (make-instance 'a :a 2))

然后,如果我想创建一个函数来获取列表中每个实例的 a 值,我会这样做

(defun get-a (lst)
  (mapcar #'a lst))

并调用它

(get-a '(b c))

但我这样做我得到一个错误:

There is no applicable method for the generic function
  #<STANDARD-GENERIC-FUNCTION A (1)>
when called with arguments
  (B).
    [Condition of type SIMPLE-ERROR]

如果我不是直接使用 mapcar 调用访问器,而是调用包含访问器的函数,也会发生这种情况。我也尝试过使用循环和其他东西而不是 mapcar。

谢谢

【问题讨论】:

  • (get-a (list b c)),不是(get-a '(b c))

标签: lisp common-lisp sbcl clos


【解决方案1】:

如果您阅读错误,您会得到解释。

There is no applicable method for the generic function
  #<STANDARD-GENERIC-FUNCTION A (1)>
when called with arguments
  (B).

所以你接到了一个电话,类似于(a 'b)。但是b 是一个符号,而不是一个 CLOS 实例。

(b c) 是两个符号的列表。您可能想要创建一个包含两个 CLOS 实例的列表。使用 LIST 创建一个包含评估参数的列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多