[Newbies] Re: Collection subclasses

nicolas cellier ncellier at ifrance.com
Thu Apr 3 19:47:53 UTC 2008


Rob Rothwell a écrit :
> If I create a simple object subclassed from Collection, 
> SequenceableCollection, or ArrayedCollection called CollectionObject, with
> 
> CollectionObject>>#initialize
>      self halt.
> 
> and execute:
> 
> CollectionObject new.
> 
> the halt is executed.
> 
> Whereas, if I subclass CollectionObject from:
> 
> Set
> Dictionary

If you examine the protocol of these classes, you will see that they use 
an #initialize: with an argument instead.

Look on the class side methods:

Set class>>new: nElements
	"Create a Set large enough to hold nElements without growing"
	^ self basicNew initialize: (self sizeFor: nElements)

> OrderedCollection

Yet another implementation of new: ... basicNew setCollection:

> Array

No extra initialization at all performed by default (it's a deliberate 
optimization).

> It does not.
> 
> Is one not supposed to make subclasses of these classes, and if so, why not?
> 
> I have an object that is most naturally a Dictionary-like object with 
> various subclasses, but I can not initialize it...
> 

Nothing prevents you to define

MyClass class>>new: n
	^self basicNew initialize: n

Or the message you want...
But yes, initialize is NOT always sent upon instance creation.

> Thank you; I feel I am missing something very basic in my understanding!
> 
> Rob Rothwell
> 

You can of course create your own subclass (only SmallInteger is really 
hardly subclassable if I remember).

Whether or not you should do it is another question...

Nicolas



More information about the Beginners mailing list