Storing and Retrieving Point

Martin McClure martin at hand2mouse.com
Fri Sep 28 03:41:17 UTC 2001


At 9:18 PM -0400 9/27/01, raymondasselin at sympatico.ca wrote:

[...]

>  >
>>  Therefore all you really  need to do is
>>  pt _ 25 at 125
>>
>snip....
>
>John
>What you say is new to me
>
>-There is lots of examples in Smalltalk which suggest something like
>Morph new, or Foo new, so for me this was not linked to the use of
>declaring the type of a variable.
>-I know that when you try to define new for a Class you defined, Squeak
>answer a caution 'new is used in the existing class system......' I
>usually ignore it and go forward.
>
>Your remark is intriguing to me...did this means that it's not a good
>way to go to define a 'new' as a 'builder' Class method in Squeak ?
>I'm a little bit 'confus' because before your note I was pretty sure
>that pt_ Point new. was the way to go !!!

Sending a class the message new is indeed the most common way of 
creating a new instance in Smalltalk. However, in this particular 
case it was not needed, because many classes define other methods 
that create instances, and Point is one of them. In fact Point is one 
of the minority of classes where #new is not even a useful way to 
create instances.

Let's take a look at the original code example:

	pt _ Point new.
	pt _ 25 at 125.

The first statement sends the message #new to the class Point, which 
creates a new Point and answers it. The variable pt is now assigned a 
reference to this new point, whose x and y instance variables are 
each nil.

The second statement sends the message #@ to the literal Number 25 
with the single argument a literal Number 125. Numbers respond to the 
#@ message by creating and answering a new Point with the receiver as 
the x coordinate and the argument as the y coordinate.

So the second statement creates a second instance of Point, this one 
with reasonable values for x and y, and assigns to pt a reference to 
this second Point. The first, unitialized, Point is now not 
referenced and will be garbage collected.
So, except for eating a little more processor time, the second 
statement alone has exactly the same effect as both statements.

Is there a way to do this that uses the more familiar pattern of 
creating an unitialized instance and then initializing it? Yes, but 
in this case it's bad form. I'll explain. You could write:

	pt _ Point new.
	pt setX: 25 setY: 125.

which creates an unitialized Point and then sends that Point a 
message that initializes it. However, this is NOT good form, since 
the method Point>>setX:setY is in Point's 'private' category. Points 
are intended to be immutable, with each instance getting its x and y 
values at the time it's created and never changing them again.


If in doubt about what message to send a particular class to make a 
new instance, browse the class-side methods of that class and its 
superclasses, by convention under the category 'instance creation'.


Hope this helps,

-Martin




More information about the Squeak-dev mailing list