s LearnSqueak.com

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.

5 minutes · 5 small steps · Squeak 6.1

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.

Select the whole expression, then Print it
OrderedCollection new
NATIVE SQUEAK CAPTURE
Native Squeak cascade checkpoint 1
Actual native Squeak Workspace Print it result.
You should see

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.

Select the whole expression, then Print it
| basket |
basket := OrderedCollection new.
basket add: 'apple'.
basket
NATIVE SQUEAK CAPTURE
Native Squeak cascade checkpoint 2
Actual native Squeak Workspace Print it result.
You should see

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.

Select the whole expression, then Print it
| basket |
basket := OrderedCollection new.
basket add: 'apple'; add: 'pear'.
basket
NATIVE SQUEAK CAPTURE
Native Squeak cascade checkpoint 3
Actual native Squeak Workspace Print it result.
You should see

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:.

Select the whole expression, then Print it
OrderedCollection new
    add: 'apple';
    add: 'pear';
    yourself
NATIVE SQUEAK CAPTURE
Native Squeak cascade checkpoint 4
Actual native Squeak Workspace Print it result.
You should see

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.

Select the whole expression, then Print it
OrderedCollection new
    add: 'apple';
    add: 'pear';
    size
NATIVE SQUEAK CAPTURE
Native Squeak cascade checkpoint 5
Actual native Squeak Workspace Print it result.
You should see

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.

Download the five examples; copy one at a time

Your first list · More Smalltalk messages