Search code examples
smalltalk

Typed collections in Smalltalk


I'm trying to learn some smalltalk programming.... I'm trying to create a list of objects of type myClass. What's the best way to do this?

I have the following:

| list |
list := OrderedCollection new.

Correct me if I'm wrong.

So how should I add elements to my list?


Solution

  • To create new instances of MyClass send the class the message #new

    MyClass new
    

    Now, to add an element to a collection, just send the collection the message #add:

    list add: MyClass new
    

    There is no such a thing as static types in Smalltalk. In other words, the equivalent of the Java ArrayList<MyClass> is just OrderedCollection.