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.

9. Control <strong><strong>an</strong>d</strong> Iteration<br />

9.1 <strong>Introduction</strong><br />

This chapter introduces how control <strong><strong>an</strong>d</strong> iteration are achieved in <strong>Smalltalk</strong>. To do this a number of<br />

concepts which are u nique to <strong>Smalltalk</strong> are also discussed (such as the block object). To simplify this<br />

process for the reader, equivalent C constructs are illustrated beside <strong>Smalltalk</strong> constructs (where they<br />

exist).<br />

The remainder of this chapter is structured in the following m<strong>an</strong>ner. Section two introduces the<br />

concept of cascading, Section three discusses blocks <strong><strong>an</strong>d</strong> Section four describes the control structures<br />

available in <strong>Smalltalk</strong>.<br />

9.2 Cascading<br />

Cascading is a syntactic form that allows multiple messages to be sent to the same object. The aim of<br />

this is to reduce the need for tediously repeating the same object over <strong><strong>an</strong>d</strong> over again, because a whole<br />

set of messages must be sent to that one object. The intention is to produce more concise code which is<br />

easier to read, <strong><strong>an</strong>d</strong> to avoid the need for additional temporary variables.<br />

Cascading is achieved by using the semicolon delimiter. This is slightly confusing for those coming<br />

from a C or Ada background as the semicolon is used there as a expression terminato r. Instead, in<br />

<strong>Smalltalk</strong>, it should be read as send the next message to the same object as the last message. For<br />

example, the expressions:<br />

c<strong>an</strong> be written in a shorter form as:<br />

Tr<strong>an</strong>script cr.<br />

Tr<strong>an</strong>script tab.<br />

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

Tr<strong>an</strong>script cr.<br />

Tr<strong>an</strong>script cr;<br />

tab;<br />

show: 'Hello World';<br />

cr.<br />

The programmer should be wary of using the cascade mech<strong>an</strong>ism. Although it was intended to<br />

simplify code, it c<strong>an</strong> often have the opposite effect. For example, in a very long cascaded expression,<br />

which appears all on the same line, it c<strong>an</strong> become difficult to determine exactly what is happening. It is<br />

therefore often the case that a set of message expressions are easier to underst<strong><strong>an</strong>d</strong> when they are not<br />

written using cascading.<br />

9.3 Blocks<br />

9.3.1 Block definition <strong><strong>an</strong>d</strong> evaluation<br />

Earlier in this book you were told to treat Blocks as the same as Begin/End constructs in Pascal <strong><strong>an</strong>d</strong><br />

ADA or {}’s in C. This was, however, not exactly the truth. They are in fact objects in their own right<br />

(<strong><strong>an</strong>d</strong> are inst<strong>an</strong>ces of BlockClosure) which represent code that c<strong>an</strong> be executed (or evaluated) now or at<br />

a later date. Block expressions therefore describe deferred activities.<br />

81

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

Saved successfully!

Ooh no, something went wrong!