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.

5.3 Inspectors<br />

Another type of tool available in the <strong>Smalltalk</strong> environment are Inspectors. Inspectors permit the<br />

internal state of <strong>an</strong> individual object to be viewed. I n particular they allow the user to view <strong><strong>an</strong>d</strong> ch<strong>an</strong>ge<br />

inst<strong>an</strong>ce variables directly. In effect inspectors are to inst<strong>an</strong>ces as browsers are to class definitions.<br />

Unlike other programming l<strong>an</strong>guage development tools, you c<strong>an</strong> use <strong>an</strong> inspector to ch<strong>an</strong>ge the state of<br />

<strong>an</strong> inst<strong>an</strong>ce (for example, to set up <strong>an</strong> object ready for testing a piece of code) as well as examine its<br />

contents.<br />

The inspector window is divided into two as shown in Figure 5.2. The left h<strong><strong>an</strong>d</strong> side s hows the<br />

inst<strong>an</strong>ce variables of the object, while the right h<strong><strong>an</strong>d</strong> side shows the contents of those variables. For<br />

example, in the figure, the prefs inst<strong>an</strong>ce variable has been selected (in the left h<strong><strong>an</strong>d</strong> side) while the<br />

dictionary contained by that inst<strong>an</strong>ce va riable is displayed (in the right h<strong><strong>an</strong>d</strong> side). If the contents is a<br />

composite object (that is <strong>an</strong> object which contains other objects e.g. such as the dictionary shown), then<br />

you c<strong>an</strong> open <strong>an</strong>other inspector which will present the contents of that object. To d o this you select the<br />

inspect option off the right (middle on the three button mouse) button menu in the left h<strong><strong>an</strong>d</strong> side.<br />

The right h<strong><strong>an</strong>d</strong> view of the window not only allows you to examine <strong><strong>an</strong>d</strong> ch<strong>an</strong>ge the contents of<br />

inst<strong>an</strong>ce variable, it also allows you to evaluate expressions (just as you c<strong>an</strong> in a Workspace) except that<br />

the evaluation happens within the context of the inst<strong>an</strong>ce. This me<strong>an</strong>s that inst<strong>an</strong>ce <strong><strong>an</strong>d</strong> class variable s<br />

c<strong>an</strong> be referenced in the same way as they c<strong>an</strong> within <strong>an</strong>y method definition.<br />

Note that two inspectors are built into the bottom of the debugger window <strong><strong>an</strong>d</strong> c<strong>an</strong> be extremely<br />

useful when attempting to decide why something unexpected has happened.<br />

The use of inspectors provides a powerful debugging <strong><strong>an</strong>d</strong> testin g tool. All objects respond to the<br />

message inspect; the basic inspector method is implemented in the class <strong>Object</strong>’s inst<strong>an</strong>ce<br />

protocol. Most objects respond by opening <strong>an</strong> inspector window, labeled with the class of the receiver.<br />

Figure 5.2: An example inspector<br />

5.4 Notifiers<br />

You should also get familiar with the system notifier, such as those you will encounter when you type in<br />

<strong>an</strong> incorrect Smalltal k expression. These notifiers are there to help you. For example, in Figure 5.3, I<br />

have mis-typed show, the system recognizes this <strong><strong>an</strong>d</strong> informs me of the fact. If I select the correct it<br />

option, it will try to find what it thinks I was trying to type. In this case it will give a select list<br />

containing the show: message, which I c<strong>an</strong> select. This will replace my misspelling <strong><strong>an</strong>d</strong> the code will<br />

successfully execute.<br />

Figure 5.3: A system notifier<br />

54

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

Saved successfully!

Ooh no, something went wrong!