[Newbies] Re: A do with ONLY index? (plus,. a style question)
Klaus D. Witzel
klaus.witzel at cobss.com
Fri Feb 9 06:58:32 UTC 2007
on Fri, 09 Feb 2007 02:17:28 +0100, you wrote:
> On Sun, 04 Feb 2007 02:16:09 -0800, Klaus D. Witzel wrote:
>> You might want to compare:
>> class side>>new
>> | instance |
>> instance := self new.
>> instance thisAndThat "extra code needed".
>> ^ instance
>> instance side>>initialize
>> "sent automatically by Behavior>>new"
>> iVar1 := 'text'.
>> iVar2 := 0
>> The latter is preferable over the former (less code, less maintenance).
>> But often people put utility methods like #on:, #with: etc on the class
>> side, for non-trivial initializations.
> When you say "sent automatically by Behavior>>new" is that true? In
> other words:
> x := abitraryObject new.
> results in an attempt to call arbitraryObject>>initialize?
Sure. Evaluate "Object halt; new" with doIt and then in the debugger, in
the DoIt method line push the buttons "Through" then "Into".
>> From the (re-)usability point of view, if you had getters/setters
>> (like in traits), the perfect approach is
>> x := MyClass new setY: 'text';
>> setZ: 0;
>> Or, like I prefer to do it
>> (x := MyClass new)
>> setY: 'text';
>> setZ: 0.
> I suppose we don't worry much about the "waste" of, say, setting up some
> features with default values and then having to discard those when the
> user sets them to something actually useful?
But yes we care :) The default initializer is Object>>#initialize, an
empty method with is executed at primitive speed (it just returns self).
Before that, #basicNew (the primitive) has already initialized the new
instance with all nil's (or zeros, depending on the class' format spec).
From the ~ 2887 classes in my Squeak-dev image, only some 928 implement
initialize (even less when subtracting all the class side implementors).
So yes, it's quite common behavior to let the user set something actually
More information about the Beginners