**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.
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:
Se puede analizar o describir un numero por medio de un codigo
or
Me parece increible que por medio de un codigo son la necesidad de usar numeros o un calendario podamos expresar la fecha actual
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`:
En la parte de este literal se puede resolver problemas matematicos por medio de unos codigos
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:
El literal nos permite graficar algunas expreciones matematicas
El literal nos permite graficar algunas expreciones matematicas con un color en especifico
Con estos literales se puede organizar datos especificos en una tabla
**NB:** `:=` is *not* a message, but the built-in assignment operator.
Es interesante estos literales premite remplazar una x por un 0
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`:
Los literales premite por medio de dos puntos representar una palabra clave
To handle more arguments, simply add more keywords:
Los literales premite afirmar los argumentos por medio de palabras claves
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:
Los literarios permite resolver problemas matematicos con las palabras claves
is equivalent to:
Interesante como por medio de otra formula permite dar el mismo resultado
Parentheses can be used to change the precedence. Note that the following expression yields `10` and not `7`:
This is easily fixed as follows:
Estos literales premiten especificar mas los problemas matematicos para un mejor resultado
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:
Estos literarios premite organizar plabras claves con unas caracteristicas especificas
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?
Can you explain the result
3x2´2´2= 6.461
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:
En la parte de literales se puede analizar el significado de una palabra a un numero
{{gtMethod:name=Number>>#squared |expanded}}
`new Integer(42).betweenAnd(41,43)`