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.

<strong>an</strong>Array at: 1 max: 10.<br />

This will try <strong><strong>an</strong>d</strong> send the message at:max: to <strong>an</strong>Array. Here it is necessary to use round<br />

brackets to ensure that it is the object returned as a result of accessing position 1 in the array which<br />

receives the max: message. For example:<br />

7.6.5 Parsing rules<br />

(<strong>an</strong>Array at: 1) max: 10.<br />

Although the order in which expressions are evaluated is left to right, there are three levels of<br />

precedence amongst the categories of message which affect this evaluation. The three categories each<br />

have a different precedence as indicated by the following table:<br />

Message category Precedence Example<br />

unary 1 size, rem, odd<br />

binary 2 *, /, - , +<br />

keyword 3 max:, min:, at:put:<br />

This me<strong>an</strong>s that a unary message will be evaluated before a binary or keyword message. In turn a<br />

binary message will be evaluated before a keyword message. If this order is not what is desired then<br />

round brackets c<strong>an</strong> be used to alter the evaluation sequences as above. For example:<br />

2 * 3 max: 3 * 4 odd.<br />

This would generate <strong>an</strong> error, because, the odd message at the end of the expression has the highest<br />

precedence. This me<strong>an</strong>s that the result of sending the message odd to the value 4 would be used as the<br />

parameter for the message ‘*’ to be sent to the value 3. As this would result is multiplying 3 by false,<br />

the system would generate a run time error. To ensure that we obtain the correct result, we might place<br />

brackets around parts of the expression, thus:<br />

((2 * 3) max: (3 * 4)) odd.<br />

This c<strong>an</strong> be a common source of errors. If you have a problem with <strong>an</strong>y of the parsing rules a quick<br />

<strong><strong>an</strong>d</strong> easy way to make sure the system parses <strong>an</strong> expression in the way that you w<strong>an</strong>t, is to put brackets<br />

around parts of the expression to describe your requirements. Then select the format menu option in<br />

one of the VisualWorks browsers. This option not only formats the code according to <strong>Smalltalk</strong><br />

st<strong><strong>an</strong>d</strong>ards, it also removes <strong>an</strong>y unnecessary brackets.<br />

7.7 Summary<br />

In this chapter you have learnt about classes in <strong>Smalltalk</strong>, how they are defined, how inst<strong>an</strong>ce variables<br />

are specified <strong><strong>an</strong>d</strong> how methods are constructed. You have also encountered m<strong>an</strong>y of the basic <strong>Smalltalk</strong><br />

l<strong>an</strong>guage structures.<br />

7.8 Further reading<br />

Two good books to have a look at now, if you have not already done so, are [Lewis 1995] <strong><strong>an</strong>d</strong> [Hopkins<br />

<strong><strong>an</strong>d</strong> Hor<strong>an</strong> 1995].<br />

75

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

Saved successfully!

Ooh no, something went wrong!