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.

Messages with one or more arguments, separated by part of the method selector <strong><strong>an</strong>d</strong> composed of<br />

alph<strong>an</strong>umeric symbols <strong><strong>an</strong>d</strong> a trailing colon, are called Keyword Messages. For example:<br />

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

10 max: 20.<br />

aDictionary at: 'UK' put: 'United Kingdom'.<br />

In the above examples show:, max: <strong><strong>an</strong>d</strong> “at:put:” are the keywords (termed selectors) . This<br />

me<strong>an</strong>s that the name of a message selector is spread amongst the arguments. These c<strong>an</strong> be <strong>an</strong>y simple<br />

identifier with a trailing colon. The argument c<strong>an</strong> be <strong>an</strong> expression representing <strong>an</strong>y object. Keyword<br />

expressions have the lowest precedence.<br />

7.6.3 Message selectors<br />

A message selector is the term used to describe the method interface provided by one object to other<br />

objects. For example, if <strong>an</strong> object possesses a method with the following definition then it possesses a<br />

message selector of “addAddress:for:”.<br />

addAddress: <strong>an</strong>Address for: aName<br />

addressBook at: aName put: <strong>an</strong>Address.<br />

Notice that the method selector only consists of the method name <strong><strong>an</strong>d</strong> does not include <strong>an</strong>y of the<br />

parameters dispersed amongst that name.<br />

7.6.4 Precedence<br />

<strong>Smalltalk</strong> has slightly different rules regarding precedence th<strong>an</strong> m<strong>an</strong>y other l<strong>an</strong>guages. For those of you<br />

who are unclear about precedence, this refers to the order in which operators are evaluated in <strong>an</strong><br />

expression. M<strong>an</strong>y l<strong>an</strong>guages, such as C, have quite complex rule s regarding precedence, which<br />

determine the order in which <strong>an</strong> expression such as:<br />

2 + 5 * 3 - 1 / 2.<br />

would be evaluated. <strong>Smalltalk</strong> is rather more intuitive. Essentially it h<strong><strong>an</strong>d</strong>les parsing in a left to right<br />

m<strong>an</strong>ner. For example, in the above example, 5 is added to 2, the result (7) is multiplied by 3 (to give<br />

21). 1 is subtracted from this (giving 20) which is divided by 2. The result of this expression is therefore<br />

10. That is, there is no precedence difference between +, -, * or /. If you wish to alter t he way in which<br />

the arithmetic calculations are performed, then you c<strong>an</strong> use round brackets. For example:<br />

2 + (5 * 3) - 1 / 2.<br />

This expression will be evaluated to the value 8. This rule also includes unary messages. For<br />

example:<br />

arraySize := #('a' 'b' 'c' 'd') size even<br />

the message size is sent to the array object, <strong><strong>an</strong>d</strong> even is sent to the resulting object. The result of this<br />

is then assigned to the variable arraySize.<br />

Keyword messages are a special case, in that although they too are parsed in a left t o right m<strong>an</strong>ner,<br />

the system assumes that all the keywords within one expression are part of the same message selector.<br />

Thus <strong>an</strong> expression such as:<br />

aDog name: 'Fido' age: 2.<br />

will be parsed as calling a message name:age:. If this is not the intention then t he programmer must<br />

indicate to the system that what is required is that the message name: <strong><strong>an</strong>d</strong> then a separate message<br />

age: should be sent to aDog. For example, either as separate messages to aDog or using the<br />

cascading mech<strong>an</strong>ism. A potential point of confu sion here is when the intention is to send the second<br />

method to the result obtained from the first. For example:<br />

74

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

Saved successfully!

Ooh no, something went wrong!