Creating an array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating an array

Duke Normandin
After a long enough hiatus, I'm re-visiting Smalltalk :)

http://www.gnu.org/software/smalltalk/manual/html_node/Arrays.html#Arrays

says:

[quote]
There is a shorthand for describing the messages you send to
objects. You just run the message names together. So we would say
that our array accepts both the at: and at:put: messages.[/quote]

Given:

x := Array new: 20
x at: 1 put: 99

What would be the shortcut syntax for this array assignment? Or am I
misunderstanding what I quoted above??  TIA...

Duke

--
Duke
“Tolerance becomes a crime when applied to evil.” ― Thomas Mann

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Creating an array

Stephen Woolerton

>
> [quote]
> There is a shorthand for describing the messages you send to objects.
> You just run the message names together. So we would say that our
> array accepts both the at: and at:put: messages.[/quote]
>
> Given:
>
> x := Array new: 20
> x at: 1 put: 99
>
> What would be the shortcut syntax for this array assignment? Or am I
> misunderstanding what I quoted above??  TIA...
>
> Duke
>
I think the quote you've referenced is just describing how messages are
communicated. For example: shorthand for other messages that Array
accepts are 'printOn: aStream' and 'replaceFrom: start to: stop with:
byteArray startingAt: replaceStart'.

What I think you want to know is how to create an array with a shortcut
syntax...

If at the time the array is created, the elements of the array are
already known, then there is a shortcut syntax:
x := { 99. } will create an array of size 1 as per your example above.

x := { 'abc'. 1. 2. 'abcd'. } will create an array of size 3 and is
equivalent to
x := Array with: 'abc' with: 1 with: 2 with: 'abcd'.       (you can only
create arrays with a max of five items using this syntax)

Arrays are of fixed size. If you want a variable size collection, use
OrderedCollection

y := x asOrderedCollection.
y := y add: 5; yourself.
y := { 'abc'. 1. 2. 'abcd'. } asOrderedCollection.
y := (OrderedCollection new) add: 1; add: 2; yourself.

Stephen

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk