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.

x inspect.<br />

This has some similarities to a hash table found in some other l<strong>an</strong>guages (e.g. Common LISP) or in<br />

libraries available for other l<strong>an</strong>guages (e.g. C). The great adv<strong>an</strong>tage of <strong>Smalltalk</strong> is that everyone has<br />

the same type of Dictionary. In Pascal o r C almost everyone would have to re -invent their own or<br />

purchase a library to get the same functionality. This, of course, leads to problems of consistency<br />

between implementations.<br />

Another form of the Dictionary collection also exists. This form is termed the<br />

IdentityDictionary. Like the IdentitySet it uses the == test rather th<strong>an</strong> the = test. This<br />

me<strong>an</strong>s that for <strong>an</strong> object to be returned, the object used as the key must be the same object as was used<br />

to create the key <strong><strong>an</strong>d</strong> not just <strong>an</strong> equivalent object. Like the IdentitySet it is more efficient that the<br />

st<strong><strong>an</strong>d</strong>ard Dictionary class <strong><strong>an</strong>d</strong> may be used in certain circumst<strong>an</strong>ces.<br />

11.8 Strings<br />

Strings are represented in <strong>Smalltalk</strong> as collections of characters. Unlike in l<strong>an</strong>guages such as C,<br />

there is no need to provide special string processing functions such as strcpy(). Instead, we c<strong>an</strong> treat<br />

strings as collections (<strong><strong>an</strong>d</strong> iterate over their contents) or as “strings” in their own right. This is because<br />

we are inheriting the collection class (<strong><strong>an</strong>d</strong> the su bclasses between the collection class <strong><strong>an</strong>d</strong> the class<br />

String) methods <strong><strong>an</strong>d</strong> are using the methods defined in the class String.<br />

Useful String operations include the concatenation of strings, searching for substrings <strong><strong>an</strong>d</strong> pattern<br />

matching between s trings. For example, sameAs: aString <strong>an</strong>swers whether the receiver <strong><strong>an</strong>d</strong><br />

aString match precisely (ignoring case differences).<br />

To concatenate two strings together use a comma. For example:<br />

myName := 'John' , ' E ' , 'Hunt'.<br />

Notice that you do not have to specify the length of the string. Remember they are just objects <strong><strong>an</strong>d</strong> a<br />

variable c<strong>an</strong> hold <strong>an</strong>y object.<br />

Substring operations are essential in <strong>an</strong>y l<strong>an</strong>guage <strong><strong>an</strong>d</strong> <strong>Smalltalk</strong> provides a variety of features for<br />

searching for substrings <strong><strong>an</strong>d</strong> pat tern matching between strings. For example, the findString:<br />

startingAt: message. This message searches the receiving string for the string passed as a<br />

parameter to the method. For example:<br />

'John Hunt was here' findString: 'Hunt' startingAt: 1.<br />

There are two pattern matching methods defined for strings, sameCharacters: <strong><strong>an</strong>d</strong> match:. The<br />

first method counts the number of beginning characters which are the same. The second method is more<br />

powerful <strong><strong>an</strong>d</strong> c<strong>an</strong> include two types of wild card ( # which c<strong>an</strong> represent <strong>an</strong>y single character <strong><strong>an</strong>d</strong> *<br />

which c<strong>an</strong> represent zero or more characters). For example:<br />

‘Dr. * Hunt’ match: ‘Dr. John Hunt’.<br />

This expression evaluates to true. There is a vari<strong>an</strong>t of the match: method which c<strong>an</strong> either ignore or<br />

consider case; match:ignoreCase: (which takes either true or false as the second argument).<br />

Inst<strong>an</strong>ces of class String also respond to boole<strong>an</strong> operators such as ‘=’. Strings c<strong>an</strong> also be converted into lower or upper<br />

case using asLowerCase <strong><strong>an</strong>d</strong> asUpperCase. Finally, as st rings are types of collection they c<strong>an</strong><br />

respond to the iteration message described above. These c<strong>an</strong> be used for performing some operation on<br />

each character in a string in turn.<br />

99

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

Saved successfully!

Ooh no, something went wrong!