This programming environment allows up-to date access to an objects instance variables,
to a classes definition,
the set of messages which are implemented there (i.e. the protocol),
the source code of those methods, the execution state and much more.
In addition, cross references can be searched
(who sends which message, who implements which message, who references a variable etc.),
breakpoints can be set and modifications can be made to a class.
Modifications to a class can be made at any time - even while there are active instances of it around;
any modification is immediately effective (that does make a difference in programmers productivity !).
In the following chapters, we make use the Pen class, which simulates a pen drawing on
a surface.
Making use of this class has two advantages:
first, it is very simple and thus easy to understand,
second and even more important: it gives a graphical feedback of what we do and is therefore
pure fun !
WorkspaceApplication open
and create a variable named "myPen" by selecting its "Add Workspace Variable"
menu item:
myView" workspace variable.
Then, in this (or another) workspace, evaluate the following code:
This creates and opens up another view (referred to by the name "myView").
In addition, it also creates a pen-instance and makes the workspace variable "myPen" refer to it.
| view |
myView := View new.
myView width: 200.
myView height: 200.
myView open.
myPen := Pen in: myView.
A pen's internal state consists (among others) of its current drawing position on the
canvas, the drawing direction and an up/down state.
Pens understand a bunch of useful messages, among them are:
- home
- positions the pen at the center of the window and set its drawing direction to "east" (i.e. left-to-right)
- north
- changes the pen's drawing direction to "north" (i.e. up)
- turn: degrees
- makes the pen turn clockwise the given number of degrees
- down
- places the pen onto its drawing canvas (i.e. followup moves will leave a drawing on the canvas)
- up
- lifts the pen from its drawing canvas (i.e. followup moves will NOT draw on the canvas)
- go: distance
- makes the pen move forward along its drawing direction the given number of pixels
- fillColor: aColorObject
- changes the pens drawing color
- clear
- clears the window
To make the pen draw a triangle, evaluate:
You get colored triangles with:
myPen home.
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
to make it draw a nice picture, try (and understand):
myView clear.
myPen home.
myPen fillColor: (Color red).
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
myPen fillColor: (Color black).
(notice the use of a block here, and the extra turn after the triangle has been draw).
myView clear.
myPen home.
18 timesRepeat: [
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
myPen go: 50.
myPen turn: 120.
myPen turn:20.
]
Another nice picture (which also shows a good use of blocks) is generated by:
|rect|
rect := [:side | 4 timesRepeat:[ myPen go:side; turn:90 ] ].
myView clear.
myPen home.
36 timesRepeat: [
rect value:60.
myPen turn:10.
]
To inspect the internals of an object, we use a so called Inspector,
which can be opened by sending the object to be inspect an "inspect"-message.
Try:
You will see (scroll down the left list if required)
that - among other - the pen keeps information about its current position
("locationX" and "locationY") and its drawing direction ("direction").
For now, ignore the other values you will see in the inspector.
myPen inspect.
Clicking on such an instance variable in the inspector shows its value in the right part of the inspectors window.
It is also possible to interactively send messages to the inspected object
from within the inspector,
by entering the message expression into the inspectors (right) code pane,
and evaluating it (using "doIt") just like in a workspace.
Within the inspector, the inspected object is referred to by the pseudo-name "self";
Therefore, we can send messages to the inspected object,
even if no global- or workspace-variable refers to it.
As an excercise, let the pen draw something using self-messages from within an inspector.
Browsers operate on the class(es) as they are present in your running system - in contrast to other so called IDEs, which operate on a classes source code as contained in a file. Changes done in a browser take effect immediately and are immediately available. There is no need to leave the system, recompile or restart the program for such changes.
There are many ways to open up a browser:
You can also browse any objects class by simply sending it a "
- - via the Launcher
- by pressing the browsers icon or selecting the "SystemBrowser" item in the classes menu.
- - via a Workspace
- by selecting an expressions text and then applying the "others"-"browseIt" popUp-menu function 1
- - via an Inspector
- the selector also provides a "browseIt" popUp-menu function for the selected private variable
- - by evaluating an expression
- the browser can also be opened by a program, by sending the "
browse" message to a class or by sending an "open"-message to the browser-class.
browse" message.
Thus, to browse your pen's class, try:
myPen browse.
Browsers are explained in detail in the "Programming Tools" section of
this online document.
For now, lets just find the Pen-class (use the "Find-Class" menu function in the browsers tool menu)
and have a look at the Pen classes full protocol
(select "*all*" in the protocol list, which is the second right list in the browsers upper area).
Excercises:
go:" and "turn"-messages
(in the Pen class) we used before ?
triangle"-message so that we can write:
myPen triangle.
and have the pen draw a triangle with sides of 50 pixel length.
In the text-area at the bottm, replace the shown text with the
code for the triangle-method, as:
The syntax is very simple: the first line defines the name of the new method (the selector),
the rest are the statements to be evaluated, when a triangle message arrives at a pen.
(in this case, a bunch of messages is sent to the receiver-pen itself).
triangle
self go: 50.
self turn: 120.
self go: 50.
self turn: 120.
self go: 50.
self turn: 120.
After you typed in (or copy-pasted) the text into the browser,
install that new method, by selecting the "accept" item of the
text-areas popUp-menu.
The browser will update its views to reflect the change - especially,
you will see colorization and other markings
in the class- and method lists. These indicate changes done to the system,
and are described in more detail in the
System Browsers Documentation.
Now, try to send this new message to our old friend, "myPen" by evaluating (in a workspace):
Notice, that the pen class has been changed for a living object - we did not have
to shutdown and restart any program.
The existing pen instance learned the new message while being alive !
myPen triangle
(That is what makes an integrated development environment.)
Excercises:
Pen-method named "triangle:" (with colon),
which takes a single argument and draws a triangle with sides of that length.
triangle:" method works, change the
"triangle"-method (without colon) to invoke that new method.
(such an action is called "refactoring" and is very common when working in Smalltalk)
From the Smalltalk perspective, these systems are not "integrated" at all.
Copyright © Claus Gittinger Development & Consulting
Copyright © eXept Software AG
<cg@exept.de>