Pergunta

I can't use the clos accessor functions when the class is in a list.

Say I have class a:

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

And I make 2 instances:

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

and then if I wanted to create a function that would get the a value for each of the instances while in a list i would do

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

and call it with

(get-a '(b c))

but I do that I get an error:

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

And it also happens if instead of calling the accessor directly with mapcar, I call a function which contains the accessor. Also I've tried using loops and other things instead of mapcar.

Thanks

Foi útil?

Solução

If you read the error, you get the explanation.

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

So you got a call, which is similar to (a 'b). But b is a symbol, not a CLOS instance.

(b c) is a list of two symbols. You probably wanted to create a list of two CLOS instances. Use LIST to create a list with evaluated arguments.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top