Messages in Smalltalk, have the form of expressions. They provide the means of communication among objects and are the way the operations of an object are requested.
Message expressions have two parts to them. One is the specification of the object that is to receive the message. The other is the message itself. The message itself specifies a selector entry, or method, in the receiver object and possibly one or more parameters. The parameters are pointers to other objects.
There are three different categories of messages: unary, binary, and keyword. Unary has no parameters, which make them the simplest kind. They have only two parts, the object to which they are to be sent and the name of the method in that receiving object. The first symbol of a unary message specifies a receiver object; the last symbol specifies the method of that object that is to be executed.
Unary Example
firstAngle sin
This message sends a parameterless message to the sin method of the object firstAngle. The sin method returns a number object that is the value of the sine of the value of firstAngle.
Binary Example
21 + 2
Binary messages have a single parameter, an object, which is passed to the specified method of the specified receiver object. In this example, the receiver object is the number 21, to which is sent the message + 2. So the messages 21 + 2 passes the parameter object 2 to the + method of the object, in this case, 23. If the system already contains the object 23, then the result is a reference to it rather than to a new object.
Keyword Example
firstArray at: 1 put: 5
Keyword expressions specify one or more keywords to organize the organize the correspondence between the actual parameters in the message and the formal parameters in the method. This message sends the objects 1 and 5 to a particular method, at:put:, of the object firstArray. The keywords at: and put: idintify the formal parameters of the method to which 1 and 5, respectively, are to be sent. The method to which this message is sent includes the keywords of the message. As mentioned earlier, keyword methods do not have names; rather they are identified by their keywords. This catenation-in this case, at:put: -is called a selector.
Combination Example
firstArray at: index - 1 put: 77
Message expressions can consist of any number of combinations of the three kinds of expressions. Unary expressions have the highest precedence, followed by binary expressions, followed by keyword expressions. Unary and binary are associate left to right. This expression sends 1 to the - method of the object index. The result of this operation, along with 77, is then sent to the at:put: method of the object firstArray.
Home