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.

They are more efficient for storage <strong><strong>an</strong>d</strong> certain logical operations (such as =) <strong><strong>an</strong>d</strong> so may be used<br />

instead of strings if their values will not be altered <strong><strong>an</strong>d</strong> substring operations are not required.<br />

7.4 Assignments<br />

A variable name c<strong>an</strong> refer to different objects at different times; assignments c<strong>an</strong> be made to variable<br />

names. The “:=” symbol is used in <strong>Smalltalk</strong> to indicate assignment . It is often read as “become equal<br />

to” although some do read it as “colon equals”. Some examples are:<br />

currentEmployeeIndex := 1.<br />

newIndex := oldIndex.<br />

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

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

together:<br />

next<strong>Object</strong> := new<strong>Object</strong> := old<strong>Object</strong>.<br />

The above example also illustrates a feature of <strong>Smalltalk</strong> style - the specification of variable names<br />

which indicate what they contain. This technique is often used where a more me<strong>an</strong>ingful name (such as<br />

currentEmployeeIndex) is not available, i.e. where temp might be used in other l<strong>an</strong>guages.<br />

It is worth reiterating the point that variables in <strong>Smalltalk</strong> are not strongly typed as in l<strong>an</strong>guages such<br />

as Pascal <strong><strong>an</strong>d</strong> ADA. Instead, <strong>Smalltalk</strong> is dynamically typed. That is, a variable i s not un-typed, rather<br />

its type is determined by its current contents. Thus, it is possible to determine the type contained in a<br />

variable by sending it a message asking for its type. Of course, by type we really me<strong>an</strong> class. It is also<br />

possible that a varia ble which is currently holding a string, may then be assigned <strong>an</strong> integer. This is<br />

quite legitimate <strong><strong>an</strong>d</strong> often happens, for example:<br />

myVariable := 'John'.<br />

myVariable := 1.<br />

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

An import<strong>an</strong>t point to note is that assignment is by reference. This me<strong>an</strong>s that in the following<br />

example nextIndex, newIndex <strong><strong>an</strong>d</strong> oldIndex all refer to the same object.<br />

new := old := (Bag new).<br />

next := new.<br />

The effect of these assignments is illustrated in Figure 7.1.<br />

next<br />

new<br />

old<br />

aBag<br />

Figure 7.1: The result of a multiple assignment<br />

As all three variables point to <strong>an</strong> inst<strong>an</strong>ce of a container class (in this case<br />

made to the contents of <strong>an</strong>y one of the variables, it would be made for all three!<br />

Bag), if <strong>an</strong> update was<br />

7.5 Variables<br />

7.5.1 Types of variable<br />

There are a number of different types of variable available in the <strong>Smalltalk</strong> l<strong>an</strong>guage. Some of these<br />

have already been discussed. The following provides a review of some of the different types:<br />

71

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

Saved successfully!

Ooh no, something went wrong!