Hello
Last night I found myself with a simple problem that in the end I
couldn't solve. I'm fairly sure there is a simple solution too, i just
couldn't finds it. Take note that it was late :)
Imagine I have an object with :accessor my-accessor. At runtime I
need to do;
(setf (my-accessor obj) a-value)
However, until runtime I do not know either the object or name of
the accessor function and setf's evaluation behaviour does not seem to
allow leaving it that late, i.e. there doesn't seem to be a way of
doing;
(defun set-my-value (obj accessor val)
(setf (acecssor obj) val))
Am I just being really dense or is there an idiom that I'v missed?
thanks
Andrew
Andrew Lawson wrote:
> Hello
> Last night I found myself with a simple problem that in the end I
> couldn't solve. I'm fairly sure there is a simple solution too, i just
> couldn't finds it. Take note that it was late :)
>
> Imagine I have an object with :accessor my-accessor. At runtime I
> need to do;
>
> (setf (my-accessor obj) a-value)
>
> However, until runtime I do not know either the object or name of
> the accessor function and setf's evaluation behaviour does not seem to
> allow leaving it that late, i.e. there doesn't seem to be a way of
> doing;
>
> (defun set-my-value (obj accessor val)
> (setf (acecssor obj) val))
You have several options:
- (setf (slot-value object slot) val)
Here, SLOT should be the name of a slot, and can be a computed value.
- Setter functions can be funcalled. So for example, you can say this:
(funcall '(setf some-accessor) new-value object)
Again, the first parameter to funcall can be a computed value, including:
(funcall `(setf ,some-accessor) new-value object)
(funcall #'(setf some-accessor) new-value object)
Setter functions always take the new-value to be assigned as the first
parameter.
Note that :before/:after/:around methods on setter functions won't be
triggered when you used the (setf slot-value) function.
When you have the CLOS MOP layer available, there are even more options,
but the ones above should cover almost all cases.
Pascal
--
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
On 2005-11-25, Pascal Costanza <ยทยท@p-cos.net> wrote:
> (funcall `(setf ,some-accessor) new-value object)
>
> (funcall #'(setf some-accessor) new-value object)
This is what I'm looking for, didn't realise you could funcall (setf
xxx).
Andrew
Andrew Lawson wrote:
> This is what I'm looking for, didn't realise you could funcall (setf
> xxx).
You can, if it's a function. You can't if it's expanded at macro
expansion time. Note that for standardized accessors it's unspecified
whether there is a function with the extended function name (setf <accessor>).
Paul
Paul F. Dietz wrote:
> Andrew Lawson wrote:
>
>> This is what I'm looking for, didn't realise you could funcall (setf
>> xxx).
>
> You can, if it's a function. You can't if it's expanded at macro
> expansion time. Note that for standardized accessors it's unspecified
> whether there is a function with the extended function name (setf
> <accessor>).
The OP talked about accessors created with the :accessor option in a
defclass form. They are specified to be (methods on) generic functions,
so they must be funcallable.
Pascal
--
My website: http://p-cos.net
Closer to MOP & ContextL:
http://common-lisp.net/project/closer/
Pascal Costanza wrote:
> The OP talked about accessors created with the :accessor option in a
> defclass form. They are specified to be (methods on) generic functions,
> so they must be funcallable.
Agree!
Paul