Storing and Retrieving Point

raymondasselin at sympatico.ca raymondasselin at sympatico.ca
Sat Sep 29 03:41:28 UTC 2001


Martin McClure <martin at hand2mouse.com> wrote:
> 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.

...each nil...
this is so evident that I didn't saw it before. With pt _ Point new.
I get an empty point.... so this is like 'declaring the type of a
variable'
as John M McIntosh said in a previous message.

> 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.

OK this is really clear now.

> 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.

That's it... I missed the fact that #@ is an instance creation message,
partly
because two numbers and the arrow _  conducted me to assimilate  the
right part of the expression (25 at 125) with an assignation of values to
the point created the line before. Or as you explained this is'nt an
assignation of values but a reference to a new instance of point.

> 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.
 
By the way, it would be very interesting if Squeak could clearly
separate public method of privates one differently than by just putting
them in a category!
 
> 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

Thank you Martin for these explanations.




More information about the Squeak-dev mailing list