5.2: Three Types of Messages
- Page ID
- 39592
Pharo distinguishes between three kinds of messages to reduce mandatory parentheses. A few simple rules based on the such different message determine the order in which the messages are sent.
There are three different types of messages:
- Unary messages are messages that are sent to an object without any other information. For example, in
3 factorial
,factorial
is a unary message. - Binary messages are messages consisting of operators (often arithmetic). They are binary because they always involve only two objects: the receiver and the argument object. For example in
10 + 20
, + is a binary message sent to the receiver 10 with argument 20. - Keyword messages are messages consisting of one or more keywords, each ending with a colon (:) and taking an argument. For example in
anArray at: 1 put: 10
, the keyword at: takes the argument 1 and the keywordput:
takes the argument 10.
Unary messages
Unary messages are messages that do not require any argument. They follow the syntactic template: receiver messageName
. The selector is simply made up of a succession of characters not containing : (e.g., factorial
, open
, class
).
89 sin >>> 0.860069405812453
3 sqrt >>> 1.732050807568877
Float pi >>> 3.141592653589793
'blop' size >>> 4
true not >>> false
Object class >>> Object class "The class of Object is Object class (BANG)"
Important
Unary messages are messages that do not require any argument. They follow the syntactic template: receiver selector
.
Binary messages
Binary messages are messages that require exactly one argument and whose selector consists of a sequence of one or more characters from the set: +, -, *, /, &, =, >, |, <, ~, and @. Note that -- (double minus) is not allowed for parsing reasons.
100@100 >>> 100@100 "creates a Point object"
3+4 >>> 7
10 - 1 >>> 9
4 <= 3 >>> false
(4/3) * 3 == 4 >>> true "equality is just a binary message, and Fractions are exact"
(3/4) == (3/4) >>> false "two equal Fractions are not the same object"
Important
Binary messages are messages that require exactly one argument and whose selector is composed of a sequence of characters from: +, -, *, /, &, =, >, |, <, ~, and @. -- is not possible. They follow the syntactic template: receiver selector argument
.
Keyword messages
Keyword messages are messages that require one or more arguments and whose selector consists of one or more keywords each ending in :
. Keyword messages follow the syntactic template: receiver selectorWordOne: argumentOne wordTwo: argumentTwo
.
Each keyword takes an argument. Hence r:g:b:
is a method with three arguments, playFileNamed:
and at:
are methods with one argument, and at:put:
is a method with two arguments. To create an instance of the class Color
one can use the method r:g:b:
(as in Color r: 1 g: 0 b: 0
), which creates the color red. Note that the colons are part of the selector.
Note
In Java or C++, the method invocation Color r: 1 g: 0 b: 0
would be written Color.rgb(1, 0, 0)
.
1 to: 10 >>> (1 to: 10) "creates an interval"
Color r: 1 g: 0 b: 0 >>> Color red "creates a new color"
12 between: 8 and: 15 >>> true
nums := Array newFrom: (1 to: 5). nums at: 1 put: 6. nums >>> #(6 2 3 4 5)
Important
Keyword messages are messages that require one or more arguments. Their selector consists of one or more keywords each ending in a colon (:). They follow the syntactic template: receiver selectorWordOne: argumentOne wordTwo: argumentTwo
.