From: Tim Bradshaw on
On 2010-04-30 17:08:32 +0100, Trastabuga said:

> How to modify the append-element function so that it doesn't know
> about the fld1 and had semantics of (append-element o el field-name)
> so we can call it like (append-element o 'a 'fld1)?

You can use SLOT-VALUE if you are happy to set the slot directly. If
you want to use the accessor (which you probably should want to do)
then I think that you can use FDEFINITION, based on the accessor's name:

(defun set-field (o accessor new)
(funcall (fdefinition `(setf ,accessor)) new o))

--tim

From: Trastabuga on
On Apr 30, 1:08 pm, Tim Bradshaw <t...(a)tfeb.org> wrote:
> On 2010-04-30 17:08:32 +0100, Trastabuga said:
>
> > How to modify the append-element function so that it doesn't know
> > about the fld1 and had semantics of (append-element o el field-name)
> > so we can call it like (append-element o 'a 'fld1)?
>
> You can use SLOT-VALUE if you are happy to set the slot directly.  If
> you want to use the accessor (which you probably should want to do)
> then I think that you can use FDEFINITION, based on the accessor's name:
>
> (defun set-field (o accessor new)
>   (funcall (fdefinition `(setf ,accessor)) new o))
>
> --tim

Thank you, Tim, very good example.
From: Norbert_Paul on
You might try #'(setf foo) to get the writer part of the accessor #'foo.
I tried it, but I don't know if this is always possible.

(defun append-element (o el getter setter)
(funcall setter (append (funcall getter o) (list el)) o)
)

(append-element o :new-el #'test1-fld1 #'(setf test1-fld1))

Norbert


Trastabuga wrote:
> Hi
>
> I have a simple task:
> Given a class, I need a function that can add elements to one of the
> fields of the class.
> It's easy to do if the function knows the name of the field, but what
> if the function doesn't know it and we need to pass it as an input
> parameter to the function.
> Let's say
> (defclass test1 ()
> ((fld1 :accessor test1-fld1 :initarg :fld1 :type list)))
>
> (setq o (make-instance 'test1 :fld1 nil))
> (defun append-element (o el)
> (with-slots (fld1) o
> (setf fld1 (append fld1 (list el)))))
>
> How to modify the append-element function so that it doesn't know
> about the fld1 and had semantics of (append-element o el field-name)
> so we can call it like (append-element o 'a 'fld1)?

From: Tim Bradshaw on
On 2010-05-02 14:19:06 +0100, Norbert_Paul said:

> You might try #'(setf foo) to get the writer part of the accessor #'foo.
> I tried it, but I don't know if this is always possible.

This works, but it only works if you know the name FOO: you can't say
(let ((fun 'foo) #'(setf fun)) for instance: you need FDEFINITION for
that.

From: Norbert_Paul on
Tim Bradshaw wrote:
> On 2010-05-02 14:19:06 +0100, Norbert_Paul said:
> This works, but it only works if you know the name FOO: you can't say
> (let ((fun 'foo) #'(setf fun)) for instance: you need FDEFINITION for that.

Did you mean

(defun append-element (o el accessor)
(let ((getter (fdefinition accessor))
(setter (fdefinition `(setf ,accessor))))
(funcall setter (append (funcall getter o) (list el)) o)))

?