[comp.lang.lisp] Common Lisp's SETF feature

whitney@endor.harvard.edu (Glen Whitney) (08/04/89)

Suppose I have a variable, FRED, whose value is a place
descriptor for SETF, like (get 'joe 'eye-color), and I
would like to SETF into the place described by FRED's
value. Is there any nice way of doing this? I couldn't
figure one out. Any help would be appreciated. 
   (I do realize that the method can't be determined 
until run-time because it might differ for
different values of FRED. I'm willing to pay any cost
associated with that. I just don't know any nice means of
telling lisp to do it.)
    Thanks
      --- Glen Whitney

Krulwich@eecs.nwu.edu (Bruce Krulwich) (08/04/89)

In article <2360@husc6.harvard.edu>, whitney@endor (Glen Whitney) writes:
> Suppose I have a variable, FRED, whose value is a place
> descriptor for SETF, like (get 'joe 'eye-color), and I
> would like to SETF into the place described by FRED's
> value. Is there any nice way of doing this? I couldn't
> figure one out. Any help would be appreciated. 

I almost break out in hives to say this, but one way would be:

    (eval `(setf ,fred ,<value descriptor>))

but I think that unless you're doing a debugging system or something like that
there must be a way around having to use the interpreter like this.  Are you
sure you haven't made some (bad) design choice that has forced you to do
horrible stuff like this??


Bruce Krulwich
Institute for the Learning Sciences

dlw@odi.com (Dan Weinreb) (08/04/89)

You definitely would need to invoke macro expansion at runtime in
order to do what you are asking for.  So the following solution is
probably as good as any.  Note that this is not incredibly efficient,
but I doubt you can do a whole lot better.  Warning: I have not
debugged this.

(defun setf-var-val (the-symbol new-value)
  (eval `(setf ,(eval the-symbol) ',new-value)))