**Understanding Smalltalk message syntax**
# TL;DR
# What is a “message”?
## Unary messages
#Binary messages
#Keyword messages
#Precedence
#Cascades
#Exercise
#Further reading
This tutorial explains the syntax and evaluation order of unary, binary and keyword messages in Pharo.
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.
**Pregunta**: ¿que pasa cuando el receptor no entiende el mensaje? ¿se manifiesta algun tipo de error?
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`.
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
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.
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:
Here the message `#between:and:` is sent to the receiver `42`, with arguments `41` and `43`. In a language like Java, this might written:
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:
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.)
What result do you expect when evaluating the following expression?
**Pregunta**: ¿al usar la secuencia de los mensajes se contruirá un objeto completo?
See also: [[Understanding Smalltalk method syntax]] and [[Smalltalk method syntax on a postcard]].
For a short video, see [[Smalltalk Syntax in 7']].
**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:
{{gtMethod:name=Number>>#squared |expanded}}
`new Integer(42).betweenAnd(41,43)`
**Pregunta** ¿es posible que al aparecer un error o falte algun caracter (,:') se pierda la construccion del mensaje que se quiere dar?