**Understanding Smalltalk message syntax**
This tutorial explains the syntax and evaluation order of unary, binary and keyword messages in Pharo.
# TL;DR
Smalltalk draws a clear distinction between “methods” and “messages”. In Smalltalk “everything happens by sending messages.” In the words of Dan Ingalls, “we have a universe of well-behaved objects that courteously ask each other to carry out their various desires.” (In “Design Principles Behind Smalltalk”, Byte Magazine, August 1981. See [[Smalltalk history]])
An object “sends a message” to another object, asking it for a service; if the receiver “understands the message”, then it has a “method” for fulling the request. The method is the request, and the method is the code to fulfil the request.
# What is a “message”?
There are *unary*, *binary* and *keyword* messages in Smalltalk. A unary message consists of a word sent to a receiver object. Here, the message `squared` is sent to the receiver, the number `2`.
**Aside.** Smalltalk convention is to refer to messages as literals, so we refer to this message as `#squared` (and not `squared`). This literal can be looked up in the method dictionary of the relevant class as follows:
**Pregunta:** Se entiende que smalltalk es un "lenguaje de programacion reflexivo orientado a objetos y tipado dinamico.", esto que significa realmente.
**Pregunta:** ¿Que se entiende por message syntax en smalltalk?
**Pregunta:** No me es clara la diferencia entre unary messages, binary messages y keyword messages. Se entiende que depende la de cantidad de palabras o caracteres usadas para generar el mensaje.
**Pregunta:** ¿De que manera influyen las mayúsculas en el mensaje?
**Pregunta:** ¿Por qué es importante diferenciar entre unary, binary and keyword messages?
{{gtMethod:name=Number>>#squared |expanded}}
We can see that `2` *understands* the message `squared` because GT shows us a little grey triangle next to the message. If you click on it, it will expand to show the method for fulfilling the reuest.
Everything is an object in Smalltalk, even classes, so you can send messages to classes too, for example:
or
# Unary messages
A binary message is an operator built up from non-alphanumeric characters, such as `+`, `-`, `,`, `>`, `<`, `=`, and so on.
A binary message expression always consists of a *receiver*, the binary *operator*, and an *argument*. For example, here the receiver is the number `3`, and the binary message is `+ 4`, consisting of the operator `+` and the argument `4`:
Note that here too, `3` understands the message `#+`, and has a (primitive) method to fulfil the request.
All binary operators in Smalltalk are just binary messages, since *everything happens by sending messages*. Some common examples:
**NB:** `:=` is *not* a message, but the built-in assignment operator.
This assigns the value `0` to `x`, but is not a message send.
# Binary messages
Keyword messages consist of a sequence of keywords, each terminated by a colon (`:`), and followed by an argument. Here, for example, the keyword message `raisedTo: 5` consists of the keyword `#raisedTo:` and the argument `5`, and is sent to the receiver `2`:
To handle more arguments, simply add more keywords:
`new Integer(42).betweenAnd(41,43)`
Here the message `#between:and:` is sent to the receiver `42`, with arguments `41` and `43`. In a language like Java, this might written:
# Keyword messages
In complex expressions, first unary messages are evaluated, from left to right, then binary, and finally keyword messages.
For example, the following expression:
is equivalent to:
Parentheses can be used to change the precedence. Note that the following expression yields `10` and not `7`:
This is easily fixed as follows:
# Precedence
An unusual feature of Smalltalk is that of *cascaded messages*. You can send multiple messages to the same object, by joining them with semi-colons. This is often used to build up a complex object using a sequence of messages:
Here we first create a new `OrderedCollection`, and then send four messages to this object. Note that sence the `add:` method returns its argument rather than the receiver, to ensure that the final result is the collection and not the literal `#blue`, we send a final message `#yourself`, which returns the collection. (This is a common idiom in Smalltalk cascades.)
# Cascades
What result do you expect when evaluating the following expression?
Can you explain the result?
# Exercise
See also: [[Understanding Smalltalk method syntax]] and [[Smalltalk method syntax on a postcard]].
For a short video, see [[Smalltalk Syntax in 7']].
# Further reading