[Newbie] Morph class>>new

Anthony Hannan ajh18 at cornell.edu
Sun Apr 13 16:24:14 UTC 2003

Hi Christian,

Christian Hofer <christian.hofer at gmx.de> wrote:
> Hello,
> I want to write a method like:
> MyMorph class>>withTitle: aString
>    ^self basicNew withTitle: aString
> with:
> MyMorph>>withTitle: aString
>    title := aString.
>    self initialize
> Now I have seen that Morph implements the method "new" another way:
> Morph class>>new
>    ^super new initialize
> instead of
>    ^self basicNew initialize
> Is there a reason for that? Is it dangerous to use the method I wrote?

You should implement

MyMorph class >>withTitle: aString
	^ self new title: aString

If your #initialize method depends on title already having a value. 
Move that dependent code into #title:.

> BTW: I got a bit lost searching for the implementor of "new" - it should 
> be Behaviour, but Object is not a subclass of Behaviour?! I suppose 
> there is a really simple answer, so forgive me the stupid question.

Object is not, but Object class is a subclass of Behavior.  When sending
a message directly to a class, ie. Object new, you are looking up
methods in its class, ie. Object class, which has a separate but similar
class hierarchy to Object.  Highlight the class button for any class in
the browser and choose 'show hierarchy' from the class pane menu. 
Notice the difference between this hierarchy and the one you see when
the instance button is highlighted.

> BTW: I often forget to call "super initialize" in my
> MyMorph class>>initialize
> methods. This sometimes leads to Squeak crashing completely (not even 
> alt-. does help) when I open MyMorph in World. Could there be something 
> done about it?

Always call super in your initialize method.  This should not be hard to
remember since the initialize protocol is not something you create
yourself, ie. you should not be calling #initialize yourself, it is
called automatically in Morph class>>new.  So if you implement
#initialize you know you are overriding a super protocol.


More information about the Squeak-dev mailing list