Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
This repeatedly eval uates the <strong>Smalltalk</strong> code in the second block while the result of the first block<br />
evaluates to true. Note that whileTrue: is a message to the first block, it is therefore a method defined<br />
in class Context. Other control structures which use blocks include r epetition e.g. timesRepeat: <strong><strong>an</strong>d</strong><br />
do:.<br />
34.5 Class Boole<strong>an</strong><br />
Class Boole<strong>an</strong>, although not exactly part of the <strong>Smalltalk</strong> syntax, is import<strong>an</strong>t enough to be<br />
considered here. It may at first seem confusing that class Boole<strong>an</strong> is not part of the l<strong>an</strong>guage <strong><strong>an</strong>d</strong> is in<br />
fact a c lass with methods defined which provide the usual logical operations. However, given<br />
<strong>Smalltalk</strong>’s commitment the the philosophy of object orientation, it is perhaps not that surprising that<br />
facilities such as Boole<strong>an</strong> are actually provided by a class definition.<br />
Once you get used to the idea it is not that str<strong>an</strong>ge <strong><strong>an</strong>d</strong> most of the time you do not need to worry<br />
about the fact. Indeed, as long as you learn the “syntax” of boole<strong>an</strong> operators you need never actually<br />
know that they are not part of the basic l<strong>an</strong>guage.<br />
The class Boole<strong>an</strong> defines the whole protocol for boole<strong>an</strong> operations. This class has two<br />
subclasses True <strong><strong>an</strong>d</strong> False which determine what should happen when different messages are sent to<br />
them. Thus the equivalent of C’s if statement in <strong>Smalltalk</strong> is :<br />
<strong>an</strong><strong>Object</strong><br />
ifTrue: [some code ].<br />
There is also the equivalent of the unless statement in some l<strong>an</strong>guages:<br />
<strong>an</strong><strong>Object</strong><br />
ifFalse: [ some code ].<br />
These c<strong>an</strong> be combined to form <strong>an</strong> if-then-else construct.<br />
<strong>an</strong><strong>Object</strong><br />
ifTrue: [ some code ]<br />
ifFalse: [some other code].<br />
Note the period is only placed after the last line. The message selector used here is actually<br />
ifTrue:ifFalse:.<br />
The usual r<strong>an</strong>ge of boole<strong>an</strong> functions have been defined such as (&) <strong><strong>an</strong>d</strong> (|) or as well as specialized<br />
versions such as <strong><strong>an</strong>d</strong>: <strong><strong>an</strong>d</strong> or: which only evaluate the second operator if necessary. For example:<br />
(a < 2) & (b > 2) And<br />
(a < 2) | (b > 2) Or<br />
(a > 0) <strong><strong>an</strong>d</strong>: [b > (6 / a)] And where the second expression is only evaluated<br />
if the first expression evaluates to true. As here,<br />
this construct may be use d as a check to ensure<br />
that erroneous operations are not performed.<br />
(a > 0) or: [B > 0] Or where the second expression will only be<br />
executed if the first evaluates to false.<br />
34.6 Collections<br />
As with class Boole<strong>an</strong>, collections are not strictly part of the Smal ltalk l<strong>an</strong>guage, however they are so<br />
import<strong>an</strong>t that they will be mentioned here.<br />
Collections are the elements used to construct data structures in <strong>Smalltalk</strong>. They allow <strong>an</strong>y object to<br />
be grouped together <strong><strong>an</strong>d</strong> m<strong>an</strong>ipulated. Complex data structures c<strong>an</strong> be built up by combining collections<br />
together. Abstract Data Types (ADT’s) c<strong>an</strong> be inst<strong>an</strong>tiated by subclasses the default collection classes.<br />
They therefore provide a very powerful data construction mech<strong>an</strong>ism.<br />
The most commonly used collection classes are:<br />
Set<br />
Bag<br />
A collection of objects in <strong>an</strong>y order. No duplicates are allowed.<br />
A collection of objects in <strong>an</strong>y order; duplicates are allowed.<br />
289