I'm studying Lisp with Lispworks 4.2 Personal Edition. I'm reading a
chapter on CLOS and I've encountered some behavior that I find confusing.
Let's say I start a new session and I do something like this:
CL-USER 1 > (defclass circle ()
((radius :writer circle-radius)))
#<STANDARD-CLASS CIRCLE 205F441C>
CL-USER 2 > (setf c (make-instance 'circle))
#<CIRCLE 205EA8FC>
CL-USER 3 > (setf (circle-radius c) 1)
Error: Undefined function (SETF CIRCLE-RADIUS) called with arguments (1
#<CIRCLE 205EA8FC>).
1 (continue) Try invoking (SETF CIRCLE-RADIUS) again.
2 Return some values from the call to (SETF CIRCLE-RADIUS).
3 Try invoking something other than (SETF CIRCLE-RADIUS) with the same
arguments.
4 Set the symbol-function of (SETF CIRCLE-RADIUS) to another function.
5 (abort) Return to level 0.
6 Return to top loop level 0.
Type :b for backtrace, :c <option number> to proceed, or :? for other
options
And then, when I go to the class browser (from the podium), and I look under
the slot description for RADIUS under class CIRCLE, I see that the
description for :writer is circle-radius, whereas what I think I need is
(setf circle-radius).
How do you usually go about defining a :writer for a slot? I (honestly)
cannot see the advantage of using :reader or :writer over :accessor, but I
think it is a good idea to know how to define them properly because I might
want to use it later on.
"Asha Das" <·······@pd.jaring.my> wrote in message
·················@news6.jaring.my...
> I'm studying Lisp with Lispworks 4.2 Personal Edition. I'm reading a
> chapter on CLOS and I've encountered some behavior that I find confusing.
>
> Let's say I start a new session and I do something like this:
>
> CL-USER 1 > (defclass circle ()
> ((radius :writer circle-radius)))
> #<STANDARD-CLASS CIRCLE 205F441C>
>
> CL-USER 2 > (setf c (make-instance 'circle))
> #<CIRCLE 205EA8FC>
>
> CL-USER 3 > (setf (circle-radius c) 1)
Should be (circle-radius 1 c). A writer is not an accessor. Use :accessor
instead of :writer.
Wade
Asha Das wrote:
>
> I'm studying Lisp with Lispworks 4.2 Personal Edition. I'm reading a
> chapter on CLOS and I've encountered some behavior that I find confusing.
>
> Let's say I start a new session and I do something like this:
>
> CL-USER 1 > (defclass circle ()
> ((radius :writer circle-radius)))
> #<STANDARD-CLASS CIRCLE 205F441C>
>
> CL-USER 2 > (setf c (make-instance 'circle))
> #<CIRCLE 205EA8FC>
>
> CL-USER 3 > (setf (circle-radius c) 1)
>
> Error: Undefined function (SETF CIRCLE-RADIUS) called with arguments (1
> #<CIRCLE 205EA8FC>).
> 1 (continue) Try invoking (SETF CIRCLE-RADIUS) again.
> 2 Return some values from the call to (SETF CIRCLE-RADIUS).
> 3 Try invoking something other than (SETF CIRCLE-RADIUS) with the same
> arguments.
> 4 Set the symbol-function of (SETF CIRCLE-RADIUS) to another function.
> 5 (abort) Return to level 0.
> 6 Return to top loop level 0.
>
> Type :b for backtrace, :c <option number> to proceed, or :? for other
> options
>
> And then, when I go to the class browser (from the podium), and I look under
> the slot description for RADIUS under class CIRCLE, I see that the
> description for :writer is circle-radius, whereas what I think I need is
> (setf circle-radius).
>
> How do you usually go about defining a :writer for a slot? I (honestly)
> cannot see the advantage of using :reader or :writer over :accessor, but I
> think it is a good idea to know how to define them properly because I might
> want to use it later on.
In this instance, circle-radius is a simple function. Try evaluating
(circle-radius 1 c) to see it work.
If you want to define a setf-form as the writer, try
(defclass circle ()
((radius :writer (setf circle-radius))))
This will let you do (setf (circle-radius c) 1).
As for when :reader and :writer are useful: You don't always want
to define accessors for a slot -- if you don't want people touching
a slot, there's no reason to tempt them by giving them an accessor.
Not exporting such accessors is a good clue to keep their hands off,
but not giving them an accessor at all (or reader or writer) is an
even stronger clue.