06.06.2013 Views

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

Computer Programming with GNU Smalltalk - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Unary, Binary and Keyword Messages<br />

Objects, Messages and Classes: Part I 21<br />

There are 3 types of messages. The difference between them is about the selectors, arguments and the<br />

precedence. What is precedence? It will be explained in a few minutes.<br />

Unary messages are messages <strong>with</strong>out any argument. It is called unary because the resulting expression<br />

involves only one object. An example for an expression <strong>with</strong> unary message would be:<br />

'Hello World!' size<br />

As you can see there are no arguments here and the overall expression involves only the string object<br />

'Hello World!'.<br />

Binary messages are messages <strong>with</strong> an argument. But the other characteristic of binary messages is that<br />

their selectors have up to 2 characters which are not alphanumeric. An example for an expression <strong>with</strong><br />

binary message would be:<br />

3 + 4<br />

+ is a selector here and 4 is the argument for it. The expression involves two integer objects of 3 and 4.<br />

+ is one character long and it is not an alphanumeric character. So it meets the requirements of being<br />

called as a binary message.<br />

Finally, keyword messages are messages <strong>with</strong> one or more arguments whose selectors are composed of<br />

alphanumeric characters. They usually have a colon at the end of each selector word which indicates<br />

that it is demanding an argument. An example for an expression <strong>with</strong> keyword message would be:<br />

3 bitAt: 3 put: 1<br />

Our selector here is bitAt:put: where our arguments are 3 and 1. Overall expression involves three<br />

objects but it could have been one or two or another number greater than three.<br />

We gave the selector and argument features of message types, but we didn't mention about precedence<br />

of them. Precedence is a concept used for deciding which one of the messages will be executed first, if<br />

there are many messages in one expression. If you cannot imagine an expression <strong>with</strong> many messages<br />

don't worry, here it is:<br />

3 + 5 bitAt: 3 put: 1 printNl<br />

As you might see, here are three messages: +, bitAt:put: and printNl. Which one should be<br />

evaluated first? It depends on precedence rules.<br />

There are some basic rules for message precedence in <strong>Smalltalk</strong>, they are:<br />

1. Unary messages are evaluated first,<br />

2. Binary messages are evaluated second,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!