Workspaces, like the one shown above, are your scratchpad. You can type pieces of Smalltalk code into a workspace and execute them immediately. Note that you can't define Smalltalk classes in a workspace (that happens in a browser), but you can define global and temporary variables, send messages to objects and look at the results. This is a very useful facility which you should find invaluable for experimenting with the system code, and for testing your own code.
If you type a Smalltalk expression into a workspace (anything will do, even something as simple as 1+1), select it using the select mouse button so that it is highlighted, and then press the operate mouse button, you will see a menu which is very common throughout the development environment. There are three important commands in the middle of this menu—do it; print it; and inspect (it). All these three commands allow you to execute (or evaluate in Smalltalk-speak) the Smalltalk expression you have got selected. The first command, do it, simply evaluates the expression and throws the result away. This is useful if you want the side-effect of the expression but are not interested in the return value. The second command, print it, does exactly what do it does, but it also prints the result in the workspace window. This is useful if you wish to see the result, but it does corrupt the workspace with extra text (although note that this is conveniently highlighted for you to delete it with the backspace key). The third command, inspect (for some reason the 'it' is missing), also does exactly what do it does, but it opens an inspector window on the object returned by the expression. We'll be looking at inspectors shortly.
You may find it useful to keep several workspaces open containing bits of code on which you are working. Also, note that the kind of
Chapter 5
things you can do in a workspace (do it; print it; inspect) you can also do in almost every Smalltalk window. This is an extremely useful facility as it allows you to execute little snippets of code wherever you might happen to be. This applies in the transcript, browsers, inspectors, debuggers, even in windows which you yourself create. Of course, things can get very untidy if you go typing random bits of Smalltalk in every window, but it can be useful not to have to find or open a workspace just to execute a single line. Just type it in any window, select it, and do it.
Earlier versions of VisualWorks also provided something called a system -workspace. This is just like an ordinary workspace, except that it is pre-filled with all sorts of useful code fragments for you to use. There is also the installation workspace which again is just a workspace that happens to be filled with code to do with 'installing' VisualWorks on your system. If you happen to be using one of these earlier versions, and if all you want is a plain workspace, be careful that that is what you ask for from the launcher.
Browsers
If you're familiar with conventional programming in which you use a text-editor to write your code and save it in files, you have probably been wondering where exactly you write Smalltalk classes. Here is the answer. The tools used for this purpose are called browsers. A browser
A system browser browsing the category Kernel-Objects, class object,
protocol printing and method
The Smalltalk Development Environment allows you to look at the source-code of a class, examine its methods and create, modify or delete both classes and methods. The development environment provides many different kinds of browser but they all have a lot of features in common. We'll look mainly at just one kind of browser—the system browser.
The diagram on page 44 shows a system browser being used to examine the source-code of a class (the class Object in this case). To understand how it works it is necessary to understand a few things about how the Smalltalk development environment organises classes and methods for browsing purposes.
To make it easier to manage the hundreds of classes in the class hierarchy, they are grouped together into groups called categories. Each category contains classes which are related in terms of their purpose. It's important to realise that categories have nothing to do with inheritance. Categories are just arbitrary groups of classes intended to help human beings find their way around the class library. Which category a class is in does not affect how it functions in any way. It only affects where it appears in the system browser.
In a similar way, the methods inside a class are organised into logical groups called protocols. Again, which protocol a method is in does not affect how it functions in any way. Protocols exist solely to partition the methods a class implements in ways which programmers find useful when thinking about the tasks the methods perform.
With this knowledge in mind we can now look at the way the system browser works. The top left pane of the window contains a list of all the categories in the image. When a category has been selected by clicking on it with the mouse, the next pane across will contain a list of all the classes in that particular category. When a class has been selected the next pane will contain a list of all the protocols in that class. When a protocol has been selected the right-most pane will contain a list of all the methods in that protocol. Finally, selecting a method displays its source-code in the large lower pane of the system browser window.
You will have noticed that below the list of classes is a pair of radio buttons which are rather confusingly labelled instance and class. These buttons control whether the browser is looking at instance methods or class methods. This distinction is sometimes referred to as the 'instance-side' versus the 'class-side*. If you are confused by this distinction try rereading chapter 2, or try thinking of class methods as the instructions which factory objects (classes) understand, and instance methods as the instructions which the objects the factories make (instances) will understand. Remember that even when instance is
selected, you're not looking at actual instances of the class. You're looking at the methods the instances of the class will understand when they are instantiated (created).
Browser Commands
Each of the four top panes of the system browser window pops up a different menu when the operate mouse button is used on them. The menus contain commands used to manipulate categories, classes, protocols and methods respectively. We'll look at many of these commands in more detail in Part EL
Each of the menus also allows you to spawn (open) another browser which looks at just the code in that particular category, class, protocol, or method. This is a useful facility which you should use when you're particularly interested in a part of the system and don't need a full system browser. In addition, the menu you get in the class pane allows you to spawn hierarchy. This opens a browser which does organise classes according to their position in the inheritance hierarchy, instead of according to which category they're in. The hierarchy browser will show just that part of the tree relevant to the class selected at the time it is opened (that is, all the class's superclasses and subclasses). If you want to see the whole tree, just open a hierarchy browser on the class Object.
Creating New Code with a Browser
The various browsers are the fundamental way in which you write and add new code to the Smalltalk system. At the end of this chapter is a worked example of how to do this which you may find useful if you haven't yet written any of your own code.
There is one slight irregularity. If you want to create a new category in the system or a new protocol in a class, you do so by using the add... command from the category or protocol menus. However, if you want to create a new class in a category or a new method in a protocol, you do so by filling in the template provided in the bottom pane when the category or protocol is selected (and no class or method is selected respectively). The important menu command here is accept which puts what you have typed through the compiler and adds it to the system code (provided there are no errors).
There are lots of other commands in the operate menus of the various browsers (including the file put as... command used to save your code to individual files instead of the whole image if you desire).
The Smalltalk Development Environment You should try to explore them as they have all been put in the system by experienced Smalltalk programmers and are all very useful in the right circumstances. Once again, Part II of this book will help you make the best use of these commands.