29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - 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.

• marries: is the message selector<br />

• thatPerson is the argument<br />

As with every message expression it returns a result, which is what is saved into the variable newStatus.<br />

34.3.4 Message types<br />

There are a number of different types of message. These are:<br />

• Unary Message have no arguments.<br />

<strong>an</strong>Array size. theta sin. 4 even.<br />

the message selector (i.e. size) c<strong>an</strong> be <strong>an</strong>y simple identifier.<br />

• Keyword Messages have one (or more) keywords, each with <strong>an</strong> argument.<br />

index max: limit<br />

<strong>an</strong>Array at: first put: ‘John’<br />

In the above examples max: <strong><strong>an</strong>d</strong> at:put: are the keywords (termed selectors). This me<strong>an</strong>s<br />

that the name of a message selector is spread amongst the arguments. These c<strong>an</strong> be <strong>an</strong>y simple<br />

identifier with a trailing colon. The argument c<strong>an</strong> be <strong>an</strong> expression representing <strong>an</strong>y object.<br />

• Binary Messages have one argument, <strong><strong>an</strong>d</strong> the selector is one or two non -alphabetic symbols. For<br />

example:<br />

3 > 4. 100 / 17. oldIndex + 1.<br />

The second character in a binary message selector c<strong>an</strong>not be a minus sign.<br />

34.3.5 Parsing rules<br />

The parsing rules of <strong>Smalltalk</strong> c<strong>an</strong> be summarized in the following points:<br />

• Multiple expressions are separated by full stops.<br />

• Unary expressions parse left to right.<br />

• Binary expressions parse left to right.<br />

• Unary expressions take precedence over binary expressions.<br />

• Parenthesized expressions (using round brackets) take precedence over unary expressions.<br />

To summarize then the precedence order is unary<br />

brackets).<br />

- binar y -keyword (plus take into account round<br />

34.4 Blocks<br />

A block is a sequence of statements whose execution is deferred. They are evaluated in the context in<br />

which they were defined. Each block is <strong>an</strong> object of class Context. The statements are enclosed in<br />

square brackets e.g. []. The block will be executed when it is sent the message “value”. The result of the<br />

last message sent is returned as the value of the block. For example:<br />

| temp |<br />

temp := [Tr<strong>an</strong>script show: ‘Hello John’.].<br />

Tr<strong>an</strong>script show: ‘Hello World’.<br />

temp value.<br />

Blocks are most commonly used with control structures such as:<br />

<strong>an</strong><strong>Object</strong><br />

ifTrue: [Tr<strong>an</strong>script show: ‘Hello Out There’.].<br />

They may also be used in iteration statements, for example:<br />

[x > 0]<br />

whileTrue: [.....].<br />

288

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

Saved successfully!

Ooh no, something went wrong!