Messages
Unary messages (no arguments)
- 3.14 sin "send sin to 3.14"
- account balance "send balance to account"
Keyword messages (>0 arguments)
- account deposit: 100 "send deposit: to account with arg 100"
- vec at: 2 Put: İcİ "send at:Put: to vec with args 2 and 'c' "
Binary messages (1 argument)
- 3 + 4 "send + to 3 with arg 4"
- 23.1 @ 40.4 "send @ to 23.1 with arg 40.4"
Precedence:
- Unary highest
- Binary
- Keyword lowest
Associativity
- Unary left-to-right
- Binary left-to-right
- Keyword right-to-left
x + y squared x + (y squared)
sq at: n + 1 Put: n squared
sq at: (n + 1) Put: (n squared)
x + y + z (x + y) + z
a max: b min: c a max: (b min: c)
[ Previous ]
[ Index ]
[ Next ]
|
Messages
There are three syntactic forms of message, distinguished by how many
arguments are being passed (the syntax is very close to Smalltalk):
Unary messages have no arguments, and consist of an identifier after the
expression denoting the receiver. Examples:
3.14 sin "send sin to 3.14"
account balance "send balance to account"
Keyword messages contain one or more keywords, each followed by an
expression that denotes the argument. Each keyword is an identifier
immediately followed by a colon. The first keyword in a keyword message
must begin with a small letter; successive keywords must begin with a
capital letter.
Examples:
account deposit: 100 "send deposit: to account with arg 100"
vec at: 2 Put: 'c' "send at:Put: to vec with args 2 and 'c'"
Binary messages are used to pass a single argument, and consist of a symbol
made of one or more non-alphanumeric characters (excluding various
special characters). The symbol appears between the receiver and the
argument. Examples:
3 + 4 "send + to 3 with arg 4"
23.1 @ 40.4 "send @ to 23.1 with arg 40.4"
When combined, the order of precedence is unary, then binary then keyword
messages. Unary and binary messages associate left-to-right, keyword messages
right-to-left. Although all binary messages have the same precedence, a binary
message cannot be sent to the result of a binary message expression unless the
messages are the same or parentheses are used to associate them.
More examples, with their parenthesized equivalents:
x + y squared x + (y squared)
sq at: n + 1 Put: n squared sq at: (n + 1)Put: (n squared)
x + y + z (x + y) + x
a max: b min: c a max: (b min: c)
Exercise: What are the values of the following expressions?
1 + 2 * 3 + 4
1 max: 4 + 3 negated min: 0
|