[squeak-dev] The Trunk: Collections-mt.851.mcz

tim Rowledge tim at rowledge.org
Wed Sep 4 21:43:22 UTC 2019


Well, to try to explain this mostly to Christoph, #basicNew should do only the primitive creation of the chunk of object memory, hence the 'basic' (and that does include some hacks for certain classes likeBoxedFloat etc). 
#new and other initial creation methods can do further things, like setting initial values of some instance variables, registering external resources, etc.

So they do not in general work equivalently, deliberately. At the most basic level, #new sends #basicNew to create the memory and then #initialize to do the ... initialisation. If that initialize does stuff your specific needs would prefer to avoid, use #basicNew and your own initialisation sequence.

As an example, consider WriteStream>on:from:to: which uses `self basicNew`. It could use #new without any difference in effect because there is no implementation of #initialize for WriteStreams until you get to the null version in ProtoObject. 

In contrast, imagine a class where the normal use needs a load of instance variable to be set up just so, but you have a need for a quite different setup and it would waste a lot of cycles to do the typical init and then overwrite almost all of it. That might justify a separate creation method that uses #basicNew and a specialised init method. 

There's also the question of self vs super in your original question. Remember, super is for case where you need to skip up the class tree starting in the superclass of *the class in which the method you are running* lives. This is *not* always the super class of the object executing the method. Lots of people get this wrong. 

There are good explanations of the real effects of this crucial point in the Blue Book etc and you really need an example class tree with at least 4 level and it's too long to type here. Suffice to say that the practical difference between `self basicNew` and `super new` can be significant.

> On 2019-09-04, at 2:07 PM, Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com> wrote:
> 
> Hmm, I just starred at method 'source with it' in a SpurVMMaker image and got
> - 252 hits for 'self basicNew';
> - 109 hits for 'super new'.
> 
> So it might depend on context... This would deserve more statistics (age of method, initials, whether new is raising an Error or not, whether instance side initialize does nothing or the wrong thing, etc...)
> 
> Le mer. 4 sept. 2019 à 22:38, tim Rowledge <tim at rowledge.org> a écrit :
> 
> 
> > On 2019-09-04, at 12:58 PM, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:
> > 
> > Thank you :)
> > 
> > A very minor question: Would you favor [self basicNew] over [super new]? It will work completely equivalently, but what is the better style?
> 
> ^super new
> 
> `self basicNew` will not always work equivalently. And I can almost guarantee it will always do the wrong thing just when you feel sure it is doing the right thing. And you will suffer Debugging Hell.
> 
> 
> tim
> --
> tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
> Why use one word when two polysyllabic agglomerates will do?
> 
> 
> 
> 
> 
> 


tim
--
tim Rowledge; tim at rowledge.org; http://www.rowledge.org/tim
Computing Dictionary: LOOP: (go to LOOP)




More information about the Squeak-dev mailing list