From: David B. Lamkins
Subject: Re: modify struct slots by name
Date: 
Message-ID: <B01CF297-11F761@206.163.124.89>
On Fri, Aug 15, 1997 4:03 PM, SDS <··················@cctrading.com> wrote:
>If I have a struct 
>(defstruct person name)
>I can use 
>(funcall (symbol-function (find-symbol "PERSON-NAME")))
>to *access* the slot.
>If I try to do 
>(setf (funcall (symbol-function (find-symbol "PERSON-NAME"))) "smith")
>I get an error message that only (funcall #'...) can be used with setf.
>So, can I modify the slot?
>

Here's a snippet from a GBB file.  Look at ftp://ftp.cs.umass.edu/gbb/ for
source code for various platforms (see the xxx-extensions.lisp files).

;;; It is a hole in Common Lisp that you can't access a slot in a structure
;;; via the slot name.  For example, if I have an instance of the following
;;; defstruct,
;;;
;;;    (defstruct (foo (:conc-name FOO->))
;;;      (a nil)
;;;      (b nil))
;;;
;;; I want to be able to get the value of slot A with a call like:
;;;
;;;    (get-structure-slot object 'A).
;;;
;;; This is because I may not know what the accessor function is for that
;;; slot.  Even if I did know the name of the accessor function, I wouldn't
;;; be able to use it to alter the value of the slot because Setf needs to
;;; know the function name at compile time.  This means that code such as
;;; the following won't work.
;;;
;;;    (defun set-structure-slot (object slot-accessor new-value)
;;;      (setf (funcall slot-accessor object) new-value))
;;;
;;; Obviously, setf shouldn't need to handle that as a place form.  This
;;; just points out the need for a general slot accessor function which
;;; will work as a place form for setf.

From: John Wiseman
Subject: Re: modify struct slots by name
Date: 
Message-ID: <arxafif3377.fsf@gargoyle164.cs.uchicago.edu>
"David B. Lamkins" <········@teleport.com> writes:
>
> On Fri, Aug 15, 1997 4:03 PM, SDS <··················@cctrading.com> wrote:
> >If I have a struct 
> >(defstruct person name)
> >I can use 
> >(funcall (symbol-function (find-symbol "PERSON-NAME")))
> >to *access* the slot.
> >If I try to do 
> >(setf (funcall (symbol-function (find-symbol "PERSON-NAME"))) "smith")
> >I get an error message that only (funcall #'...) can be used with setf.
> >So, can I modify the slot?
> >
> 
> Here's a snippet from a GBB file.  Look at ftp://ftp.cs.umass.edu/gbb/ for
> source code for various platforms (see the xxx-extensions.lisp files).
> 
> ;;; It is a hole in Common Lisp that you can't access a slot in a structure
> ;;; via the slot name.

It would be nice if slot-value worked on structures (it's even
setf'able).  It does work in Macintosh Common Lisp.  Unfortunately I
could not understand what the HyperSpec had to say on the matter or if
it said anything at all; There's a sentence about slot-value's
undefined behavior for conditions and structures, but I couldn't tell
whether that was just in the case of a missing slot.


John Wiseman
From: Rainer Joswig
Subject: Re: modify struct slots by name
Date: 
Message-ID: <joswig-ya023180001908971511330001@news.lavielle.com>
In article <···············@206.163.124.89>, "David B. Lamkins"
<········@teleport.com> wrote:

> On Fri, Aug 15, 1997 4:03 PM, SDS <··················@cctrading.com> wrote:
> >If I have a struct 
> >(defstruct person name)
> >I can use 
> >(funcall (symbol-function (find-symbol "PERSON-NAME")))
> >to *access* the slot.
> >If I try to do 
> >(setf (funcall (symbol-function (find-symbol "PERSON-NAME"))) "smith")
> >I get an error message that only (funcall #'...) can be used with setf.
> >So, can I modify the slot?
> >

How about using SLOT-VALUE?

(defstruct car type)

(let ((car (make-car)))
  (setf (slot-value car 'type) 'porsche)
  (slot-value car 'type))

-- 
http://www.lavielle.com/~joswig/