1
votes

This is a real beginner question I guess, but I couldn't find the answer here. My problem: I would like to set the value of a slot of a class like so:

(setf (accessor class) value)

I wrote a small function for this:

(defun set-class-slots (class slot value)
      (setf (slot class) value))

I'm using this in a for loop, in which I'm iterating through 2 lists (slots (a list of symbols) and values (a list of numbers)) and would like to set several slots of an instance to the values.

(loop for slot in slots for value in values do
      (set-class-slots <myclass> slot value)
      )

The error I'm getting is:

"Undefined operator (setf slot) in form ((setf slot) #:|Store-Var-773597|#:g773598."

I think the problem is that the setf in my function does not use the value provided for the input arg. 'slot' but reads 'slot' as an operator.

I tried different things, symbol-function, funcall, etc. but don't know anymore what to do - as I also don't really understand what is going wrong.

Any help would be greatly appreciated.

Thanks, Marleynoe

1
Any reason you're not using SLOT-VALUE? - Barmar
I'm working inside a lisp-based environment and for the classes I need to modify unfortunately SLOT-VALUE does not work (i.e. slot remains unmodified). - marleynoe
What environment is that? Is it a Common Lisp implementation? If not, please update the question's tags accordingly. - acelent

1 Answers

3
votes

You can use FDEFINITION to get the function value of a (setf XXX) function:

(defun set-class-slots (class slot value)
    (funcall (fdefinition `(setf ,slot)) value class))