Fwd: Re: Storing and Retrieving Point

John M McIntosh johnmci at smalltalkconsulting.com
Fri Sep 28 03:31:09 UTC 2001


>Date: Thu, 27 Sep 2001 20:25:09 -0700
>To: raymondasselin at sympatico.ca
>From: John M McIntosh <johnmci at smalltalkconsulting.com>
>Subject: Re: Storing and Retrieving Point
>Cc:
>Bcc:
>X-Attachments:
>
>>
>>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 !!!
>>
>>Raymond
>
>Ok, well this can be a complicated subject. When you create an 
>instance you face the issue of instance creation and how the 
>designer of the class wants you to do it. If he has a "complete 
>instance creation method", usually found in the class instance 
>creation category, then you can use that.
>
>Doing a new on the class then poking at the instance might also 
>work, but then again it might not correct.
>
>For a simple example
>
>thing _ OrderedCollection with: #foo
>
>versus
>thing _ OrderedCollection new.
>thing add: #foo.
>
>Both achieve the same results, but as you see they are different.
>
>A more complex example might be
>thing _ ComplicatedDataBaseConnector useHost: 'database.apple.com'
>
>If you invoke new against ComplicatedDataBaseConnector it might not 
>do everything you need and you need to invoke other methods against 
>the instance that require more knowledge to arrive at the same 
>state. Also if someone wants to change how the instance creation 
>method useHost: worked, then you could be impacted if you had new 
>and various other methods to achieve the same state. Some classes 
>for example forbid the use of new because of this issue.
>
>
>new and overriding new.
>
>You get the warning because you are messing with an issue. Typically 
>you add a new method to your class to do ^super new initialize
>
>This is to allow the inherited class the ability to run new, then 
>you run your initialization to set anything up that is required. Go 
>take a look at OrderedCollection new: versus new.
>
>new does
>^self new: 10
>
>new: anInteger  does
>^ super new setCollection: (Array new: anInteger)
>
>
>In this case as you see new actually invokes new: which then invokes 
>super new and pokes at itself to set instance variables. One could 
>have had an instance side initalization method aka ^super new 
>initialize: anInteger. to do this instead, but 20 years back it was 
>written the way it was.
>
>basicNew
>I seem to recall various folks have written lots of papers on this 
>so I'm not going to attempt much discussion here, but some people 
>feel that at some point in a root class you need to invoke ^super 
>basicNew initialize. to reduce redundant initialization of instance 
>variables. Also there are other reasons for invoking basicNew.
>
>
>But back to your question about Point new the correct way, if not 
>for the fact we get an instance of Point by using the @ (technically 
>it's a binary operator @ against a instance of Number which invokes 
>primitive 18) you could either do
>
>pt _ Point x: 100 y: 200.
>
>or
>pt _ Point new.
>pt setX: 100 setY : 200
>
>Both are correct, but one is more elegant.
>
>Hint try 10 @ 'hello'
>is always interesting to invoke on people, something about what is a 
>language definition, versus usage of the definition.
>
>--
>
>--
>===========================================================================
>John M. McIntosh <johnmci at smalltalkconsulting.com> 1-800-477-2659
>Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
>===========================================================================


-- 
--
===========================================================================
John M. McIntosh <johnmci at smalltalkconsulting.com> 1-800-477-2659
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
===========================================================================




More information about the Squeak-dev mailing list