(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?
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
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*))))
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/