About KCP and automatic initialize
Richard A. O'Keefe
ok at cs.otago.ac.nz
Wed Sep 17 04:52:00 UTC 2003
Stephen Pair <stephen at pairhome.net> wrote:
Sorry if this has already been suggested (I haven't been able to
keep up with this thread), but what about having a prototypical
instance associated with each class?
(A) Yes, that's a nice idea. It's also one that's fairly easily
programmed:
Add a class variable Prototypes to Object.
Add the following methods to Object class:
newCopy
^self prototype shallowCopy postNewCopy
prototype
Prototypes ifNil: [Prototypes := IdentityDictionary new].
(Prototypes includes: self) ifFalse: [
Prototype at: self put: self basicNew initializePrototype].
^Prototypes at: self
Add the following methods to Object:
initializePrototype
^self
postNewCopy
^self
(B) This means that someone designing a class for which zero-argument
initialisation makes sense can just implement #initializePrototype,
on the instance side, not the class side.
(C) The #postNewCopy method is required for objects that have to be
registered when they are created.
(D) Of course there's a bit more work to do concerning what happens when
classes are changed (when should the prototype be replaced? after any
change to the class, or only after changes to the number/names of
instance variables?) and deleted.
(D) The problem is that there are fewer classes for which zero-argument
initialisation makes sense than you might imagine, especically if
you put Morph and its descendants to one side. This approach really
does nothing for them.
This is one of the objections to #new-calls-#initialize: it only
helps the minority of classes for which zero-argument initialisation
makes sense and leaves the rest exactly where they stood.
In contrast, Andreas Raab's proposal handles any kind of instance
creation equally well.
More information about the Squeak-dev
mailing list
|