What's wrong with (setf (slot-name object) new-value) ?!?
If you want more flexibility wrt naming accessors, it may be better to
switch to CLOS classes.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
> * (defun setslot (object slot-name new-value)
> (funcall (fdefinition `(setf ,slot-name)) new-value object ))
I'm not sure of portability, from CLHS:
"The mechanism by which defstruct arranges for slot accessors to be usable with
setf is implementation-dependent; for example, it may use setf functions, setf
expanders, or some other implementation-dependent mechanism known to that
implementation's code for setf."
Regards, Szymon.
Pass a function that does the job for you, like this:
(error 'slot-already-set ....
:setter (lambda (... parameterize as needed ...)
(setf (slot-name object) value)))
> Helu. Given the name of an accessor, how best to set the value
> programatically? For example:
...
> * (defun setslot (object slot-name new-value)
> (funcall (fdefinition `(setf ,slot-name)) new-value object ))
> SETSLOT
...
> Is this (i.e. guessing the setf function name, and funcalling its
> fdefinition) the only route available? Do you know any other way to do
> this?
Not in any portable fashion for DEFSTRUCT slots.
If you use CLOS, then you can use SLOT-VALUE which is SETF-able:
(setf (slot-value object slot-name) new-value)
Some Common Lisp implementations use CLOS for structures and there
slot-value will work for defstruct object, but there is no reason to
believe this has to be so.
--
Thomas A. Russ, USC/Information Sciences Institute
> Helu. Given the name of an accessor, how best to set the value
> programatically? For example:
>
> * (defstruct struct1 slot1)
> STRUCT1
>
> * (setq $a (make-struct1))
> #S(STRUCT1 :SLOT1 NIL)
>
> * (defun setslot (object slot-name new-value)
> (funcall (fdefinition `(setf ,slot-name)) new-value object ))
> SETSLOT
>
> * (setslot $a 'struct1-slot1 10)
> 10
>
> Is this (i.e. guessing the setf function name, and funcalling its
> fdefinition) the only route available? Do you know any other way to do
> this?
> --
> Madhu
defstruct defines it's own accessors
(defstruct name
(slot1 nil)
(slot2 nil))
(defparameter *name* (make-name))
(setf (name-slot1 *name*) value))
If you don't like the default name you can use :conc-name
(defstruct (position-info (:conc-name pos-))
(found nil)
(previous nil)
(space nil))
(setf (pos-found *var*) t)
Works for individual variables too.
For more flexibillity use defclass.
The advantage of defstruct is mainly that it has less overhead.
--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/