ABSOLUTE BEGINNER
Your first cascade: send more messages to one object
Use a semicolon to talk to the same object again in five tiny Squeak steps.
STEP 1
Make a small collection
Open a Workspace from the desktop menu. Type the expression, select all of it and choose Print it. OrderedCollection is a list that can grow. Replace the whole Workspace contents before each new step, including the previous printed answer.
OrderedCollection new
The printed answer is an empty OrderedCollection.
STEP 2
Add one word
A variable remembers our collection. The add: message puts a word into it. The final line asks Print it to show the collection.
| basket |
basket := OrderedCollection new.
basket add: 'apple'.
basket
You should see one word: apple.
STEP 3
Talk to the same basket twice
The semicolon means send the next message to the same receiver. Both add: messages go to basket. The period ends the whole cascade.
| basket |
basket := OrderedCollection new.
basket add: 'apple'; add: 'pear'.
basket
You should see apple and pear, in that order.
STEP 4
Ask for the basket itself
yourself answers the receiver itself. It lets this compact cascade finish by returning the collection, rather than the word answered by the final add:.
OrderedCollection new
add: 'apple';
add: 'pear';
yourself
The result is still the collection, not just pear.
STEP 5
Ask the same basket its size
Here the last message is size, so the answer is a number. A cascade keeps the receiver the same; its final message chooses the result.
OrderedCollection new
add: 'apple';
add: 'pear';
size
You should see 2. Try adding one more add: message before size.
YOU DID IT
A small step. A real result.
A semicolon sends the next message to the same receiver. The final message provides the answer. Use yourself when you want the object back.
Optional code download: open it as text and copy one example into Workspace.