-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Hello!
In chapter 7.1.5, the fourth bullet of shared-initialize is:
"Any slots indicated by the second argument that are still unbound at this point are initialized according to their :initform forms."
There is no hint that "slots" could refer only to local slots, so I think that combined with the following from chapter 7.1.4:
"If a slot has both an :initform form and an :initarg slot option, and the initialization argument is defaulted using :default-initargs or is supplied to make-instance, the captured :initform form is neither used nor evaluated."
there seems to be a strong hint that the behaviour you describe is actually not as intended by the specification: :initform forms should only be evaluated during shared-initialize.
As to your style question: I think that a class-allocated slot expresses primarily the invariant that this value is always the same for each instance. I see no problem with the encapsulation idea, though.
Best wishes Svante
Am 05.01.2011 23:08, schrieb Didier Verna:
rm@tuxteam.de wrote:
On Wed, Jan 05, 2011 at 06:15:34PM +0100, Didier Verna wrote:
Tim Bradshaw pointed out that my message was not very clear because the test I gave does not demonstrate that an initform's result is used to initialize the slot early, only at which time a slot is initialized.
This on the other hand gets closer to the point:
(defclass test () ((slot :allocation :class :initform t :initarg :slot)))
(defmethod initialize-instance :before ((test test) &key) (format t "Slot value: ~A~%" (slot-value test 'slot)))
Fresh SBCL: CL-USER> (make-instance 'test) Slot value: T #<TEST {B8115E1}>
Fresh SBCL again: CL-USER> (make-instance 'test :slot nil) Slot value: T #<TEST {B92AC89}>
So it appears that even when an initarg is provided, the slot is still initialized to the iniform value prior to anything else.
Ecuse my ignorance, but what are you testing? During your second make-instance call the (initialize-instance :before ...) method is _not_ picking up the initform but the actual slot value from the class (remember: slot has class allocation and it's value gets initialized in the first invocation of make-instance 'test).
No, I said "Fresh SBCL /again/". SBCL is restarted brand new so that both of the calls to make-instance above are creating the /first/ instance of that class.
- -- Svante Carl v. Erichsen Wentorfer Str. 96 21029 Hamburg
+49-(0)40-34923721 +49-(0)160-6941474 Svante.v.Erichsen@web.de