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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Blocks are represented by square brackets ‘[ ]’ <strong><strong>an</strong>d</strong> obviously have a very different me<strong>an</strong>ing to round<br />

brackets ‘( )’. This c<strong>an</strong> be yet <strong>an</strong>other point of confusion for those new to <strong>Smalltalk</strong>. The round brackets<br />

will return the result of evaluating what they contain, at the point at which the expression is<br />

encountered. The square brackets will return a block object. This block object will contain zero or more<br />

statements. These statements will only be executed when the block is evaluated. This is achieved by<br />

sending the message value to the block. For example:<br />

[counter := counter + 1] value.<br />

However, if we don’t w<strong>an</strong>t to execute the statements in this block until later, we don’t have to. In<br />

fact as the block is actually <strong>an</strong> object, we c<strong>an</strong> assign the block to a variable. Then when we are ready we<br />

c<strong>an</strong> evaluate the block held by the variable. For example:<br />

myBlock := [counter := counter + 1].<br />

....<br />

myBlock value.<br />

Thus when a block expression is encountered, the statements enclosed are<br />

not executed<br />

immediately. In other words, the contents of the square brackets are stored until a value message is sent<br />

to it. Thus in the above example, counter will only be incremented when the message value is sent to<br />

myBlock.<br />

It is import<strong>an</strong>t to note that the block will execute within the context in which it was defined <strong><strong>an</strong>d</strong> not<br />

necessarily in the c urrent context. This me<strong>an</strong>s that the value used for counter will be the value it had<br />

wherever the block was created.<br />

As was explained in Chapter 6 all expressions return a value. In the case of a block, when it is<br />

evaluated, the result returned is the resul t of evaluating the last expression in the block. Thus the value<br />

18 is assigned to the variable result in the following example:<br />

9.3.2 Block parameters<br />

result := [2 * 4. 3 * 6].<br />

Blocks c<strong>an</strong> also take parameters. This is done by pre ceding the statements in the block by a vertical bar<br />

<strong><strong>an</strong>d</strong> the input parameter. This parameter has a preceding colon. For example:<br />

[:counter | counter * 2.]<br />

This block takes one input parameter <strong><strong>an</strong>d</strong> possesses a single statement which multiples the input<br />

value by 2. This c<strong>an</strong> then be evaluated with a single parameter. This is achieved by sending the block<br />

the keyword message value: ; the argument is the value to bind to the block parameter counter.<br />

For example:<br />

[:counter | counter * 2] value:10.<br />

This would produce the value 20. It is possible to pass in more th<strong>an</strong> one parameter to a block. For<br />

example, if we w<strong>an</strong>t to specify that a block takes two parameters, then we could use the following<br />

definition:<br />

[:x :y | x * y ]<br />

This block would be evaluated using the value:value: message.<br />

[:x :y | x + y] value: 10 value: 20.<br />

The same number of arguments must appear in the block <strong><strong>an</strong>d</strong> the message. In fact there are also<br />

value:value:value: <strong><strong>an</strong>d</strong> value:value:value:value: messages for blocks which take<br />

three <strong><strong>an</strong>d</strong> four arguments. There is also a valueWithArguments: message for blocks taking more<br />

th<strong>an</strong> four arguments.<br />

82

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

Saved successfully!

Ooh no, something went wrong!