Getting started

Goals: This lab will show you how to get started in Smalltalk. As we learned during Scott's talk, Smalltalk is run on a virtual machine. At Grinnell, we use a commerical version of the Smalltalk virutual machine called VisualWorks.

We will first introduce VisualWorks and then you people will get to try writing small programs -- "Hello, world" and a factorial program.


Welcome to VisualWorks


1. Starting VisualWorks.
To start VisualWorks from the command line, type:

% visualworks /usr/local/visual/image/visual.im

Two small windows will pop up: the main window and a workspace. The main window and the workspace window are shown below:



2. Writing some very simple expressions.
We will not be looking at all of the menu and button options in the main window, but right now you can use the workspace and practice writing some simple Smalltalk expressions.

Try typing:

3. The System Browser
This is the last part of the initial introduction to VisualWorks. In the next two sections, we will use the System Browser to create, edit and run our programs; so, we will not need Emacs to write programs. The Browser provides useful ways to view system and user classes, it also has many features that help the user to quickly and easily develop classes, protocols, and methods.

To open a System Browser, select Browse->All Classes from the main window or use the shortcut button . Notice that a System Browser is divided into four columns across the top half of the window, and the bottom half contains a text area.

The first column is the Categories column; the classes are first organized by categories, to make it easier to manage them (and there are hundreds of them). These categories are completely arbitrary, so you can reorganize the classes any way you want. After you select a category, a list of the classes in that category will appear in the the second column, the Classes column. When you select a class, the members of the Protocols column appear; these are just groups of methods. Select a protocol and finally, the methods of the class will show in the last column, Methods.

Have fun exploring VisualWorks on your own, but the help system doesn't seem to work. Notice also, that when you quit VM it will prompt you to "save" something -- type in /home/your_name/me.im. Now, when you start VW again you just have to type visualworks me.im and all of your work from the last session has been saved!


"Hello, World"

In this section, you will learn how to make a new class using the System Browser.

  1. Middle-click in the first column and select add from the pop-up menu to create a new category. Call it Test.
  2. In the lower half of the browser, you will see a generic class definition. You will need to write in new information, so that the definition looks like this:
            Object subclass: #HelloWorld
                instanceVariableNames: ''
                classVariableNames: ''
                poolDictionaries: ''
                category: 'Test'
    
  3. Middle-click in the coding area (where you just finished typing) and select accept. This compiles the definition and creates the new class HelloWorld.
  4. Go to the third column and middle-click, selecting add to add a new protocol. You can name the protocol printing fun.
  5. Now go to the coding area again and write in your method.
         print 
            ^'Hello, World'
    
  6. Use accept to compile the method and we're done!
  7. To run your program, go to the workspace and create an instance of the class HelloWorld.
            Hi := HelloWorld new. /* middle-click mouse and select "inspect" */
            Hi print. /* middle-click and select "print it" */
    
    After you inspect the first part, VisualWorks will prompt you to declare Hi as a global or temporary variable. Choose global, so that when you run the second statement, you will not have to highlight the first statement.

Factorial in Smalltalk


The factorial program will be a method for the Integer class.

So to begin, first open the System Browser. Click on Magnitude-Numbers in the first column. You should then see the subclasses ArithmeticValue, Double, FixedPoint, etc. in the second column. Now, click once on Integer. Then, in the third column click factorization and division. You will see several methods there already: an iterative factorial, gcd, icm, etc.

You are now ready to begin writing your first factorial program in Smalltalk. Go to the coding area in the lower half of the window and write:

recFact
self < 0
     ifTrue : [^0]
     ifFalse: [
         self = 0 
             ifTrue : [^1]
             ifFalse: [^self * (self -1) recFact]
              ]

created May 3, 1996
last revised May 5, 1999

smalltalk_group@ac.grin.edu