Statements, Cascades & yourself

Statements are separated by periods (a := 10.

9.1 Statements

Statements are separated by periods (a := 10. b := a * 2. b printString); the last one may omit it. ^ expr returns from a method.

9.2 Cascades

A cascade (;) sends several messages to the same receiver — the receiver of the last message before the first semicolon:

Without cascades you would repeat the receiver: Transcript show: 'Hello'. Transcript cr. ...

Transcript show: 'Hello'; cr; show: 'World'; cr.

| list |
list := OrderedCollection new.
list add: 1; add: 2; add: 3.        "all three add: go to list"

9.3 The yourself idiom

A cascade returns the result of its last message. add: returns the *added element*, not the collection — so use yourself to return the receiver:

yourself simply returns self. You will see it constantly in constructors and builders.

(OrderedCollection new add: 1; add: 2) .               "→ 2 (result of the last add:)"
(OrderedCollection new add: 1; add: 2; yourself).      "→ an OrderedCollection(1 2)"

9.4 Cascade scope

In OrderedCollection new add: 3; add: 4; yourself, the cascade receiver is the result of OrderedCollection new — the receiver of the first message (add:) in the cascade.