[Newbies] Array vs OrderedCollection

Bert Freudenberg bert at freudenbergs.de
Mon Apr 27 13:05:04 UTC 2009

On 27.04.2009, at 14:12, Miguel Enrique Cobá Martínez wrote:

>> I had the idea that the array was internally something special and  
>> diferent than the other clases.

It is not a special kind of class, but Array is different than  
OrderedCollection from the VM's point of view.

If you look at the class definition of Array you see it uses  
"variableSubclass:" whereas OrderedCollection uses the "simple"  
"subclass:" definition. That means that OrderedCollection only has  
"named" fields (its instance variables), but Array has "indexed" fields.

If you use #at: with an OrderedCollection, it sends #at: to its  
instance variable named "array". Using #at: on an Array accesses that  
indexed field directly. So to Smalltalk code the both behave quite  

If a VM primitive method works on an object, it accesses its fields  
directly and not by sending messages. So if a primitive method expects  
an object with indexed fields, you cannot substitute an  
OrderedCollection. But it does not have to be an instance of Array,  
any class with indexable fields would do (*).

So Array is not special, you can make your own kind of indexable  
collection using the "variableSubclass:" definition. This is rarely  
needed (much easier to use Arrays) but you can do it.

- Bert -

(*) unless the primitive author (no pun intended) took a shortcut and  
actually declared the function to require an Array instance. The  
correct way is to check wether the class has the right format, not its  
class name.

More information about the Beginners mailing list