Dead Against It

Now, if you have a long expression, involving a few objects and several messages, you’re going to have to work out what’s being sent to who. Fortunately, the rules are quite simple:

1) Message types that take the fewest arguments have the highest priority

Let’s recap:

  1. Unary messages, like displayNl take no arguments.
  2. Binary messages, like << take exactly one argument.
  3. Keyword messages, like displayOn: take one or more arguments.

So:

Transcript << aFile size

The first message is size, sent to aFile (assume that’s a File object). The next message send is << to Transcript, which we know all about.

Transcript display: aFile size

Again, the first message send is size to aFile. The next is display: to Transcript.

Transcript display: aFile size * 1024

Again, the first message send is size to aFile. The next is * to the file size, an Integer, with the argument 1024 - yes, that’s right, multiplication is a message, just like any other message. display:, as ever, comes last.

2) Messages of the same type are sent in order from left to right

'Hello World' class selectors printNl!

The first message send is class, sent to ‘Hello World’. The result is the class String.

Next is selectors, sent to String. The result is a Set, containing all of the names of the messages (the message selectors) that String provides*.

Finally, the message printNl, which, like displayNl, prints the receiver on the transcript**.

3) To change this order of precedence, use brackets

aFile size * 2 raisedTo: 10 = (2 x file-size)10
aFile size * (2 raisedTo: 10) = aFile size x 1024

Back: Everyone says ‘Hi!’
Forward: Future Legend

* Strings also understand all the messages provided by the superclasses of String.

** The result of this message is the Set again. By default, if an object doesn’t return anything else in response to a message, it will return itself, which allows you to send another message to it with a minimum of fuss.