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.

34. Appendix: The <strong>Smalltalk</strong> L<strong>an</strong>guage Syntax<br />

34.1 Basic syntax<br />

34.1.1 Statement separation<br />

Achieved in <strong>Smalltalk</strong> by the<br />

example:<br />

period or full stop rather th<strong>an</strong> the semi -colon as in Pascal or C. For<br />

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

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

Although it is unnecessary to put a period at the end of the last line of code, as in Pascal it is a good<br />

idea.<br />

34.1.2 Assignment operator<br />

The a ssignment operator is the colon equals combination as in other l<strong>an</strong>guages such as Pascal. For<br />

example:<br />

a := 2 + 4.<br />

This operator is not a “copy” operator <strong><strong>an</strong>d</strong> thus may result in two variables referencing the same<br />

object. Not that := is not a message as var iables are not objects (whereas + is a message as it relates to<br />

two objects even if one is contained within a variable).<br />

Further example are:<br />

oldIndex := 1.<br />

newIndex := oldIndex.<br />

myArray := #(1 2 3 4).<br />

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

Assignments return values (like other expressions), so that several assignments c<strong>an</strong> be made together:<br />

34.1.3 Comments<br />

nextIndex := newIndex := oldIndex.<br />

Comments are defined using “ ”. Everything between the double quotes is treated as a comment. Note<br />

that comments c<strong>an</strong>not be nested in Sma lltalk. This c<strong>an</strong> be awkward if you wish to comment out some<br />

code for later.<br />

34.1.4 The return operator<br />

This operator is used to return a value from a method. By default a method will return the object that the<br />

message which triggers the method was sent to. Howeve r, this c<strong>an</strong> be altered using the return operator<br />

^. For example:<br />

^24<br />

would cause a method to return the value 24. Note that the method will “return” as soon as it encounters<br />

a return operator. It is therefore possible, although not necessarily a good ide a, to have more th<strong>an</strong> one<br />

284

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

Saved successfully!

Ooh no, something went wrong!