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

Initializing slots based on other slot values in Common Lisp Object System class definitions

14 views
Skip to first unread message

Paul Reiners

unread,
Sep 1, 2010, 12:40:41 PM9/1/10
to
In my class definition, I want to initialize one slot based on the
value of another slot. Here is the sort of thing I would like to do:

(defclass my-class ()
((slot-1 :accessor my-class-slot-1 :initarg slot-1)
(slot-2 :accessor my-class-slot-2 :initform (list slot-1))))

However this doesn't compile:

1 compiler notes:

Unknown location:
warning:
This variable is undefined:
SLOT-1

warning:
undefined variable: SLOT-1
==>
(CONS UC-2::SLOT-1 NIL)

Compilation failed.

Is there a way to do this?

Zach Beane

unread,
Sep 1, 2010, 12:46:28 PM9/1/10
to
Paul Reiners <paul.r...@gmail.com> writes:

Here's one method of doing it:

(defmethod initialize-instance :after ((instance my-class) &rest initargs)
(setf (my-class-slot-2 instance) (list (my-class-slot-1 instance))))

Sonya Keene's "Object-Oriented Programming in Common Lisp" is a good
book for learning this sort of thing.

Zach

Paul Reiners

unread,
Sep 1, 2010, 1:38:17 PM9/1/10
to

jimka

unread,
Sep 2, 2010, 1:36:31 AM9/2/10
to
I always worked under the assumption that you cannot be sure which
order the slots initialized. Is that wrong? How do you know that
shared-initialize will initialize slot-1 before it evaluates
the :initform of slot-2?

That's particularly troubling in the example from
http://stackoverflow.com/questions/3620249/initializing-slots-based-on-other-slot-values-in-common-lisp-object-system-class

------included--------

(defmethod initialize-instance :around ((self-ref self-ref) &key)
(let ((*self-ref* self-ref))
(when (next-method-p)
(call-next-method))))


(defclass my-class (self-ref)
((slot-1 :accessor slot-1-of :initarg :slot-1)
(slot-2 :accessor slot-2-of
:initform (slot-1-of *self-ref*))))


Pascal Costanza

unread,
Sep 2, 2010, 5:10:38 AM9/2/10
to
On 02/09/2010 07:36, jimka wrote:
> I always worked under the assumption that you cannot be sure which
> order the slots initialized. Is that wrong?

No, it's correct, there is no guarantee in what order slots are
initialized. The suggestion below is thus a very shaky solution and
shouldn't be trusted.

Pascal

> How do you know that
> shared-initialize will initialize slot-1 before it evaluates
> the :initform of slot-2?
>
> That's particularly troubling in the example from
> http://stackoverflow.com/questions/3620249/initializing-slots-based-on-other-slot-values-in-common-lisp-object-system-class
>
> ------included--------
>
> (defmethod initialize-instance :around ((self-ref self-ref)&key)
> (let ((*self-ref* self-ref))
> (when (next-method-p)
> (call-next-method))))
>
>
> (defclass my-class (self-ref)
> ((slot-1 :accessor slot-1-of :initarg :slot-1)
> (slot-2 :accessor slot-2-of
> :initform (slot-1-of *self-ref*))))
>
>


--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/

0 new messages