Messages & Precedence

The selector of a keyword message is the concatenation of its keywords: at:put:, between:and:.

7.1 Three kinds of messages

receiver selector 5 factorial, 'abc' reversed, Date today receiver op argument

3 + 4, a >= b, 'a', 'b', 3 @ 4, x -> y receiver key1: arg1 key2: arg2

array at: 1 put: 'x', 3 between: 1 and: 5 The selector of a keyword message is the concatenation of its keywords: at:put:, between:and:.

Binary selectors are made of one or more special characters: + - * / // \\ < > = <= >= ~= == ~~ , @ -> & | ==> and others.

7.2 Precedence rules

Parentheses first.

Unary messages (left to right).

Binary messages (left to right) — no arithmetic precedence! Keyword messages.

2 + 3 * 4.                  "→ 20, not 14! binary messages go left to right: (2 + 3) * 4"
2 + (3 * 4).                "→ 14"
2 raisedTo: 3 + 1.          "→ 16: binary first, then keyword: 2 raisedTo: 4"
3 factorial + 4 factorial.  "→ 30: unary first: 6 + 24"
Transcript show: 10 printString, ' items'.  "unary, then binary (,), then keyword (show:)"
#(1 2 3) inject: 0 into: [ :a :b | a + b ].  "one keyword message inject:into:"

7.3 Combining keyword messages

Two keyword messages in a row need parentheses, otherwise they parse as one selector:

Array new: 3 withAll: 0.                    "one message new:withAll: — correct"
(OrderedCollection new) add: 5; yourself.
dict at: #a put: (list at: 1).              "parentheses required: otherwise at:put:at:"

7.4 Message chains

'hello world' asUppercase reversed.               "→ 'DLROW OLLEH'"
(1 to: 10) asArray reversed first.                "→ 10"
((1 to: 10) select: [ :n | n even ]) size.        "→ 5"

7.5 Parsing exercise

How does 3 + 4 factorial between: 10 and: 2 ** 5 parse?

Unary: 4 factorial → 24.

Binary left to right: 3 + 24 → 27; and 2 ** 5 → 32.

Keyword: 27 between: 10 and: 32 → true.