开发者

Typed collections in Smalltalk

开发者 https://www.devze.com 2022-12-08 03:38 出处:网络
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'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?


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.


Before answering the It's important to emphasis that there is no strong typing in Smalltalk. Every variable is an instance of some Class of object. But the class of object can change frequently over the lifecycle of the object and neither the interpreter nor the compiler will care.

Enter the following code into the Workspace (or in the "Playground" on Pharo 4.0 and up, or the command line in GNU Smalltalk)

aNumber := 3 . "new instance of Class SmallInt is created"
aNumber := 22/7 . "aNumber is now an instance of Class Fraction"   
aNumber := 3.14159 . "aNumber is now an instance of Class Float"  
aNumber := 'Pi' . "aNumber is now an instance of Class ByteString"

No warnings or exceptions will be raised for any of these statements.

Now that is out of the way,

how should I add elements to my list?

This depends on the type of list you are using.

An Array is an integer-indexed list of fixed size. An OrderedCollection is an integer-indexed list of variable size. A Set is a collection of unique objects. A Dictionary is a Set of Association objects, i.e. key-value pairs A SortedCollection is a list of objects, sorted based on a sort block definition.

They each have unique methods of adding items.

The commonest methods for the standard collections are -
add: (not available for Array or String - as they cannot have their number of elements changed, after initial creation) at:put: (not available for Set, as in effect it only contains keys, but no values)

OrderedCollection also has addFirst:, add:after:, add:before:, add:beforeIndex:

If you send an adding message to a collection, but the collection does not understand that particular message, it will raise an exception.

So, for your list

| list newElement1 newElement2 newElement3 |
list := OrderedCollection new .
newElement1 := 'ABC' . "a ByteString"
newElement2 := 123 . "a SmallInt"
newElement3 := Dictionary new .
newElement3 at: 'Doh' put: 'A deer, a female deer' ;
            at: 'Ray' put: 'A drop of golden sun' ;
            at: 'So' put: 'A needle pulling thread' .
list add: newElement1 ;
     add: newElement2 ;
     add: newElement3 .

would result in
list (an OrderedCollection) [3 items] ('ABC' 123 aDictionary [2 items] ( 'Doh'->'A deer, a female deer' 'Ray'->'A drop of golden sun'))

0

精彩评论

暂无评论...
验证码 换一张
取 消