1

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

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
marleynoe
  • 121
  • 3
  • Any reason you're not using `SLOT-VALUE`? – Barmar Apr 25 '15 at 00:24
  • 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 Apr 25 '15 at 02:09
  • What environment is that? Is it a Common Lisp implementation? If not, please update the question's tags accordingly. – acelent May 04 '15 at 09:54

1 Answers1

3

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))
Barmar
  • 741,623
  • 53
  • 500
  • 612