Messages
There are three kinds of messages: unary, binary, and keyword. All expressions are evaluated left to right, unless you add parentheses to change the order of evaluation. Working left to right, unary messages are evaluated first, followed by binary messages, and then keyword messages.
Unary messages
Unary messages are messages with no arguments. Try these examples using Display from the System Transcript's pop-up menu, so you can see each answer.
'Quick cat' size.
In this example, the message size is sent to the string object 'Quick cat'. The answer is the object 9.
In a single statement, you can send a series of messages. The answer to the first message is the receiver of the second message, and so on, until the entire expression is evaluated.
3.6 rounded squared.
In this example, the message rounded is sent to the object 3.6, and the answer is the integer 4. Next, the message squared is sent to the object 4. The final answer is the integer object 16.
Binary messages
Binary messages, such as arithmetic expressions and comparisons, require two objects. When several binary messages are coded in an expression, the messages are evaluated left-to-right. None of the arithmetic operators has higher precedence than any other, so use parentheses to ensure that the calculation is correct. Try these examples using Display so you can see each answer:
7 + 3. "Answers 10"
7 + 4 * 5. "Answers 55"
7 + (4 * 5). "Answers 27"
57 <= 96 "Answers true"
'abc', 'def' "Concatenates two strings"
Keyword messages
Keyword expressions have one or more arguments. A keyword is a selector name followed by a colon (:). Every keyword takes an argument, and a keyword message can contain any number of keyword and argument pairs.
Some examples are:
aStream put: 'Hello World'.
databaseAgent query: aRow with: aCollection.
Cascaded messages
If you want to send a series of messages to the same object, use cascaded messages. With cascaded messages, you can write the name of the receiver once, and write each message separated by a semicolon (;) instead of a period. End the last message in the series with a period.
Cascaded messages are frequently used when you want to create a new instance of an object and initialize it at the same time. For example:
| aCollection |
aCollection := OrderedCollection new.
aCollection add: 'Owl';
add: 'Wren';
add: 'Parrot'.
Last modified date: 01/29/2015