Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

programatically constructing the #'(setf accessor) function

0 views
Skip to first unread message

Andreas Politz

unread,
Feb 1, 2009, 7:47:08 AM2/1/09
to
Hi,

assuming there is class foo with slot and accessor 'attribute'.
This makes it possible to read the value

(let ((reader 'attribute))
(funcall reader (make-instance 'foo)))

. But how do I best get at the corresponding #'(setf attribute) method
? Currently I am doing it like in the following code, but have a bad
feeling about it.

(defclass foo nil
((attribute
:accessor attribute
:initform 0)))

(defun make-undo-fn (object accessor)
(let ((value (funcall accessor object)))
(eval `(lambda nil
(funcall #'(setf ,accessor)
(quote ,value)
,object)))))

(setf obj (make-instance 'foo)
undo-fn (make-undo-fn obj 'attribute))

(attribute obj)
=> 0

(setf (attribute obj) 42)
=> 42

(funcall undo-fn)
=> 0

(attribute obj)
=> 0

-ap

Tobias C. Rittweiler

unread,
Feb 1, 2009, 8:00:58 AM2/1/09
to
Andreas Politz <pol...@fh-trier.de> writes:

> Hi,
>
> assuming there is class foo with slot and accessor 'attribute'.
> This makes it possible to read the value
>
> (let ((reader 'attribute))
> (funcall reader (make-instance 'foo)))
>
> . But how do I best get at the corresponding #'(setf attribute) method
> ? Currently I am doing it like in the following code, but have a bad
> feeling about it.

(FDEFINITION `(setf ,attribute))

-T.

Andreas Politz

unread,
Feb 1, 2009, 5:42:10 PM2/1/09
to

Thanks !

-ap

0 new messages