From: John Thingstad
Subject: Re: Setting a value programatically given the name of an accessor.
Date:
Message-ID: <op.tmr20nnqpqzri1@pandora.upc.no>
On Wed, 24 Jan 2007 13:13:06 +0100, Madhu <·······@meer.net> wrote:
> 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/