About KCP and automatic initialize

Avi Bryant avi at beta4.com
Fri Sep 12 07:38:59 UTC 2003


On Fri, 12 Sep 2003, ducasse wrote:

> > initializeWithX: xNumber y: yNumber
> >   super initialize.
> >   x := xNumber.
> >   y := yNumber.
> >
>
> This practice is not really good.
>
> should not it be:
> > initializeWithX: xNumber y: yNumber
> >   self initialize.
> >   x := xNumber.
> >   y := yNumber.

No.  That breaks the model (and, as you point out, would cause infinite
recursion with the overriden #initialize below).

There's a very distinct structure these conventions are aiming for, which
is that at each level in the hierarchy, there is one method that actually
embodies the core initialization knowledge for the object - it is what
actually enforces the invariants.

But this is *not* #initialize in every case.  Instead, it is the method
with the most information, that is, with the most or most general
arguments.

All of the other methods in that layer must point towards it.  It then
points to the layer above.

So you get something like this, with * marking the designated method at
each level and x marking the others:

             *
             ^
             |
 x --> x --> * <-- x <-- x
             ^
             |
 x --> x --> * <-- x <-- x

> I do not remember: do class methods in object-c static (not method
> lookup = more constructor like in Java) or real methods?

They're real.  The object model for Objective-C is taken pretty much
unchanged from Smalltalk.  It's where I first learned to appreciate
metaclasses.

Avi



More information about the Squeak-dev mailing list