[Q] new and initialize
Ned Konz
ned at bike-nomad.com
Tue Apr 1 23:38:25 UTC 2003
On Tuesday 01 April 2003 03:23 pm, Brian Brown wrote:
> What is the relationship between new and initialize? I see examples
> like:
>
> var := SomeClass new initialize.
>
> and also
>
> var := SomeClass new.
>
> Does initialize automatically get called upon object creation,
No. It is called explicitly in some classes.
> or
> is just another message that I can chose to call or not? I see
> classes both with and without initialize and I can understand there
> may be no instance setup required or maybe a superclass does it....
Tradition! In many cases, #new calls initialize.
For instance, in Morph, we see this pattern. So Morph subclass
constructors shouldn't call initialize if they call new (because we'd
be initializing twice).
Look at how #new is defined. In Behavior>>new we call the primitive,
as in #basicNew.
So one idiom for new is
self basicNew initialize
which would avoid the double initialization that would occur if a
superclass defined new to call initialize as well.
But it doesn't make any sense to have a 0-argument constructor in many
classes.
--
Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE
More information about the Squeak-dev
mailing list
|