Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Syntax and Messages

Unary, Binary and Keyword Messages and Precedence in Smalltalk

Understand the three kinds of Smalltalk messages and their precedence rules: unary first, binary next, keyword last, left to right. Learn when parentheses are needed and avoid the common traps.

Sending messages is the one operation of Smalltalk, so reading and writing message expressions fluently is the most valuable skill you can build. There are only three kinds of message and one precedence rule. This lesson teaches both, then shows how to avoid the classic traps with a runnable evaluator written in JavaScript.

The Smalltalk code here is read-only (no runner on this site).

The three kinds of message

Kind Form Arguments Examples
Unary receiver selector none 5 factorial, 'abc' size, Date today
Binary receiver op argument exactly one 3 + 4, a , b, x @ y, 3 = 4
Keyword receiver key1: a key2: b one per keyword 3 max: 9, array at: 2 put: 'x'
  • A unary selector is a plain identifier.
  • A binary selector is one to three punctuation characters such as + - * / // \\ < > = == ~= , @ ->.
  • A keyword message has one or more parts ending in a colon. The complete selector is all the keywords joined: at:put:. The arguments sit between the parts, which makes calls read like sentences: dictionary at: #name put: 'Ada'.

The precedence rule

When several messages appear in one expression the order is:

  1. Parentheses first.
  2. Then unary messages.
  3. Then binary messages.
  4. Then keyword messages.

Inside each level, messages are evaluated left to right. There is no other precedence: * does not bind tighter than +.

SMALLTALK
2 + 3 * 4.            "-> 20     ((2 + 3) * 4)"
2 + (3 * 4).          "-> 14"
10 - 2 - 3.           "-> 5      ((10 - 2) - 3)"
3 + 4 factorial.      "-> 27     (3 + (4 factorial))  unary before binary"
#(3 1 2) size + 1.    "-> 4"
2 raisedTo: 3 + 1.    "-> 16     binary 3 + 1 first, then the keyword message"
2 max: 3 max: 4.      "-> error! this is ONE keyword message max:max:"
(2 max: 3) max: 4.    "-> 4"

The last pair shows the most common mistake: keyword parts are not separate sends. 2 max: 3 max: 4 tries to send max:max:, which does not exist, so the debugger opens.

flowchart LR
    A["2 raisedTo: 3 + 1"] --> B["unary: none"]
    B --> C["binary: 3 + 1 = 4"]
    C --> D["keyword: 2 raisedTo: 4 = 16"]

Evaluation order follows the levels, not the position of the words.

Why this rule?

It looks strange at first, but it is consistent: a simple mechanical rule that never depends on which operators you have defined. Because you can define your own binary selectors in any class (for example + for a Money class), a fixed rule keeps programs predictable. In return, you write parentheses around arithmetic that mixes + and *.

A helpful reading technique: underline from the left, resolving unary, then binary, then keyword. Add imaginary parentheses and the code reads like a calculator.

SMALLTALK
Transcript show: 3 factorial printString , ' items'; cr.

Step by step: 3 factorial (unary) gives 6, printString (unary) gives '6', , ' items' (binary) gives '6 items', and then the keyword message show: is sent to Transcript, with a cascade cr afterwards. No parentheses are needed because unary beats binary beats keyword.

Common traps

You write You meant What happens
1 + 2 * 3 7 9: left to right
x max: y max: z max of three Error: max:max: is not understood
Transcript show: 'n = ' , n text plus number Error: a number is not a string; use n printString
a > b ifTrue: [...] works as intended Right: binary > first, then keyword ifTrue:
a ifTrue: [...] and: [...] two conditions Wrong: use (a and: [b]) ifTrue: [...]
3 + 4 printString '7' 4 printString first, so 3 + '4' fails

If something unexpected happens, parenthesize. Experienced Smalltalkers use parentheses liberally.

Binary selectors you will meet

SMALLTALK
3 + 4.  3 - 4.  3 * 4.  3 / 4.     "arithmetic"
7 // 2.  7 \\ 2.                    "integer quotient and modulo"
3 < 4.  3 >= 4.  3 = 4.  3 ~= 4.   "comparison"
a == b.  a ~~ b.                    "identity"
'ab' , 'cd'.                        "concatenation"
3 @ 4.                              "makes a Point"
1 -> 2.                             "makes an Association (key -> value)"

You can define your own in any class, such as Vector >> + other.

JS Stand-in: evaluate with Smalltalk precedence

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

No output.

JS Stand-in: your own binary operator

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

No output.

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the value of 3 + 4 * 2 in Smalltalk?

    Binary messages go left to right, so (3 + 4) * 2 = 14.
  2. 2. Which message is evaluated first in: 2 raisedTo: 3 factorial + 1 ?

    Unary messages come first. 3 factorial is 6, then 6 + 1 = 7, then 2 raisedTo: 7 = 128.
  3. 3. What is the full selector of the message in: array at: 1 put: 'x' ?

    All the keyword parts together form the single selector at:put:.
  4. 4. Why does 2 max: 3 max: 4 fail?

    It is parsed as one keyword message max:max:. Write (2 max: 3) max: 4.

Summary

  • Three kinds of message: unary (no argument), binary (one argument, punctuation selector), keyword (named parts with colons).
  • Precedence: parentheses, then unary, then binary, then keyword; left to right inside each level.
  • There is no special priority for * over +; add parentheses.
  • Keyword parts belong together as one selector, so nest keyword sends with parentheses.
  • Convert numbers with printString before concatenating them to text.

Next: Cascades, Assignment and Statements