Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Syntax and Messages

Cascades, Assignment and Statements in Smalltalk

Use Smalltalk cascades to send several messages to one receiver, understand yourself, chained assignment, statement values and returns, and write clear multi-line code with good layout.

Once you can build single message sends you need to combine them: send several messages to the same object, store results in variables, and write sequences of statements that read well. This lesson covers the cascade (the semicolon), the helper message yourself, assignment details, and the formatting conventions Smalltalkers follow.

Smalltalk snippets are read-only on this site; JavaScript stand-ins are runnable.

The cascade

A cascade sends several messages to the same receiver, the receiver of the first message in the chain:

SMALLTALK
Transcript show: 'Name: '; show: 'Ada'; tab; show: 'Age: '; show: 36 printString; cr.

Without the cascade you would write Transcript show: ... five times. The semicolon means: "the next message goes to the object that received the previous one".

What is "the receiver of the previous message" can be subtle:

SMALLTALK
OrderedCollection new add: 1; add: 2; add: 3.

The first message sent is new to OrderedCollection, then add: 1 to the new collection... or is it? The cascade applies to the receiver of the last message in the first part. Here the first part is OrderedCollection new add: 1: the last message there is add: 1, and its receiver is the new collection. So add: 2 and add: 3 also go to the collection, which is what you want. But the value of the whole cascade is the value of the last message, add: 3, which returns the argument 3, not the collection!

That is why you see yourself:

SMALLTALK
| list |
list := OrderedCollection new add: 1; add: 2; add: 3; yourself.
list.       "-> an OrderedCollection(1 2 3)"

yourself simply returns the receiver. Ending a cascade with it makes the whole expression evaluate to the object that was built. Forgetting it is one of the first bugs every Smalltalker writes.

flowchart LR
    N["OrderedCollection new"] --> C(("collection"))
    C -->|"add: 1"| C
    C -->|"add: 2"| C
    C -->|"add: 3"| C
    C -->|"yourself"| R["result = the collection"]

Every cascaded message goes to the same object; yourself makes it the result.

Cascade or separate statements?

Use a cascade when several messages configure or feed one object and the reading stays easy:

SMALLTALK
Transcript show: 'a'; cr; show: 'b'; cr.

Use separate statements when you need intermediate results:

SMALLTALK
| total |
total := 0.
#(1 2 3) do: [ :n | total := total + n ].
Transcript show: total printString; cr.

Assignment

SMALLTALK
| a b |
a := 3.
b := a + 1.
a := b := 10.      "chained: both become 10"

Assignment is an expression whose value is the assigned value, so chaining works from right to left. Assigning copies the reference, not the object:

SMALLTALK
| x y |
x := OrderedCollection new.
y := x.
y add: 5.
x.    "-> OrderedCollection(5): x and y name the same object"
y := x copy.   "now y is an independent copy"

Arguments and block parameters are read-only: foo: x x := 3 inside a method is a compile error ("Cannot store into").

Statements and the value of a script

A series of statements separated by periods runs in order. The value shown by Print it is the value of the last statement:

SMALLTALK
| a |
a := 5.
a * a.      "-> 25"

A stray period after the last statement is harmless; an empty statement is allowed. A common mistake is a missing period, which glues two lines together: x := 3 y := 4 becomes a parse error.

Returning from a method

In a method, ^ expression returns immediately:

SMALLTALK
isEven: n
    n = 0 ifTrue: [ ^ true ].
    ^ n \\ 2 = 0

A ^ inside a block returns from the method that contains the block, not just from the block. This is called a non-local return and is what makes patterns like detect:ifNone: and early exit from loops work. Details in Blocks and closures.

Formatting conventions

Pharo has a standard layout, applied automatically by the Format command (Ctrl+Shift+F in the browser):

SMALLTALK
average: numbers
    "Answer the average of a collection of numbers."
    | total |
    total := 0.
    numbers do: [ :each | total := total + each ].
    ^ total / numbers size
  • Method selector and arguments on the first line, body indented one level.
  • A comment describing the method in double quotes, right after the selector.
  • Temporaries declared at the top, one declaration line.
  • Block parameters named for what they are (each, element, item), spaces inside brackets.
  • Long keyword messages: one keyword per line, indented.
SMALLTALK
dictionary
    at: #name
    put: 'Ada'.
JS Stand-in: cascades and yourself

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: references, not copies

Press Run (or Ctrl + Enter) to see the console output here.

No output.

When you build an object with a cascade and store it, always finish the cascade with yourself. Also remember that most mutators (add:, at:put:, addAll:) return their argument, not the receiver.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In OrderedCollection new add: 1; add: 2, which object receives add: 2?

    A cascade sends every message to the receiver of the last message in the first part: the new collection.
  2. 2. What does yourself do at the end of a cascade?

    The value of a cascade is the value of its last message. yourself answers the receiver.
  3. 3. After x := OrderedCollection new. y := x. y add: 5. what is x?

    x and y refer to the same object. Use copy to get an independent collection.
  4. 4. What does ^ do inside a block within a method?

    A caret inside a block returns from the method where the block was written.

Summary

  • A cascade (;) sends further messages to the same receiver as the first part's last message.
  • The value of a cascade is the value of its last message, so end with yourself when you want the object.
  • Assignment returns the value, copies references (use copy for a new object) and cannot target arguments.
  • The result of a script is its last statement; ^ returns from a method, and from inside a block it returns from the method.
  • Follow the standard layout: selector, comment, temporaries, statements.

Next: Control Flow with Messages