October 2003
   Page 29 of 64  
 


 

Squeak, the Smalltalk of the 21st. Century


 

Germán Arduino

Smalltalk is pure objects, a technology that was created nearly 30 years ago in the Xerox laboratories of Palo Alto (PARC), and which is still in use nowadays, although the masses of programmers and corporations have not turned to it. However, its working paradigm is perhaps the best that exists to model the reality of any domain that you want to solve through software.

This article tries to show the main (and unique) characteristics of Squeak, a portable and open-source Smalltalk, whose virtual machine (VM) is written entirely in Smalltalk. I will also briefly comment on the general features of any Smalltalk.
 

Introduction

Any Smalltalk is not simply a programming language, but a place where things live; and it has a language.

Everything in Smalltalk is an object, and all in this world works with the object - message metaphor.

Any Smalltalk, including Squeak, is composed of a VM (virtual machine), specific for the operating system where it runs, and an image.

The image is the place where the objects live, environment objects like the compiler, the debugger, etc., and the objects of the solution domain.

In some cases the domain objects can reside in the basis of objects, or (not recommended, but possible) in relational databases.

The images are interchangable between VMs, that is, between operating systems.

All code in this article was developed and tested in a Mandrake 9.1 (Kernel 2.4.21) running Squeak 3.5 image 5180 (and VM level 3.4-1 for Linux). However, it was also tested under Windows XP in the same (dual boot) machine, and it is from this latter that I obtained the images for this article.

The complete development environment, for any one of the supported operating systems, can be downloaded from:

http://www.squeak.org.

How to start Squeak

Installing Squeak is beyond the scope of this article, but it is quite simple in Windows, since you only have to uncompress the complete contents of the ZIP that you download into a directory, and double-click on the executable.

In the case of Linux, you have to install the packages according to the distribution which you have, or compile the VM (this is the more difficult path).

Then, after installing Squeak, we can develop a simple script to start Squeak, for instance:

#!/bin/sh /usr/local/bin/squeak /home/garduino/squeak35/squeak35.image

Then, you can create a link to this script to place it onto the desktop. Of course, with the necessary permissions.

If all works correctly, you will see something like this:

   Page 29 of 64 

 

 

October 2003
   Page 30 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


After a little work (also beyond the scope of this article), we can obtain a more comfortable and friendly Squeak, like this one:

How to evaluate code

In the Smalltalk terminology, evaluating code means something like executing. An object called workspace allows to execute Smalltalk code, and another one, called transcript, shows the output.

We open a workspace by pressing Alt+k, or on the World menu (left-click on World, the main place of the image), selecting open, followed by workspace. We also open a transcript from the World menu: open - transcript, or pressing Alt+t.

A small piece of code for testing

   Page 30 of 64 

 

 

October 2003
   Page 31 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


Write, in the workspace:

Transcript show: 'Hello World'; cr.

Select what you wrote, and press both mouse buttons together or only the right button if you are using Windows), and select do-it, as in:

Simply, we are telling the transcript object that it should show us the string 'Hello World', using the message show: and that it should then send a carriage return.

Some surprising examples

Factorial of a large number: After the very brief introduction we gave, it's time to see numbers in Squeak. For this, type the following in the workspace: (Remember that in Smalltalk, every instruction ends with a period).

1000 factorial.

and we evaluate it, but using "print-it" instead of "do-it".

Quite fast, isn't it? But, how fast? Try the following (evaluating it with "print-it"):

Time millisecondsToRun: [1000 factorial].

As you might guess, we are sending the Time object the messages "millisecondsToRun:", that is, the time required to execute the block of code that follows (1000 factorial, in this case). The result is 30, and it is only 4000 for calculating the factorial of 10000 in an old 500 Mhz Pentium III, with 128 MB RAM. Without much development, we can produce these results!

¿Does Squeak talk?

An original 'Hello World': Make sure that you have the sound volume in your computer adequately adjusted, and write the following in your workspace:

Speaker man say: 'Hello World'.
Speaker woman say: 'Hello World'.

and evaluate each one.

Using this package, text2speech, made in Squeak, we have developed a simple WebReader.

What does 'WebReader' mean? Briefly, it means the following:

  • The user selects a URL.
  • WebReader navigates to this URL, examinates the home page, and searches an html title tag.
  • If it doesn't find this tag, WebReader sais: 'This pages doesn't have a title'.
  • If it finds the title tag, it examines it, searching for the word Squeak, and if it doesn't find it, WebReader says: 'This page doesn't mention Squeak'.
  • If the Squeak word exists in the title tag, WebReader reads the complete title outloud.
   Page 31 of 64 

 

 

October 2003
   Page 32 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


It can be more complex to parse the entire page, due to tables, and other things such as flash, css, frames, etc., but with additional work, it is perfectly possible.

First steps in the development of WebReader

In Smalltalk, the first thing you have to carry out is the 'model', the heart of the software, without user interface.

A programmer can work as he likes in Smalltalk, but a very good metaphor is the one called 'MVC', model-view-controller, that establishes the separation and independence between the kernel code and the presentation and interface for the user. This makes it possible to change the interface without affecting the system's logic.

Details of 'MVC' are beyond the scope of this article, but I mention it because of the importance it has in software development, especially in the Smalltalk world. Also, it is highly recommended that any programmer understand this model well. One step in this direction is to study design patterns, also a fundamental subject to create robust and durable system architectures. A very common pattern used in 'MVC' is the one called Observer, and that is all that this article will say about this subject.

The first thing, to get started, is to created a new project, Morphic, Menu World - open - morphic project. Morphic is a graphical framework to work in Squeak.

The new project is created as 'unnamed'; click with the mouse on 'unnamed' and type the desired name, WebReader in this case. Click on the project and a new world opens.

Menu world - open - browser or Alt+B opens the Class Browser, the tool most commonly used by any smalltalker.

In the lower half of the browser, type the code of the new class WebReader, making it a subclass of Model or Object (Model is necessary for the previously used MVC paradigm), assigning the new category WebReader, and placing the instance variables url, urlPage, parsedPage and pageTitle in it. After typing all this, press both mouse buttons together (or the right button, if you use Windows), and select accept, as shown here:

Now we already have our new WebReader class in our Squeak image.

   Page 32 of 64 

 

 

October 2003
   Page 33 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


Each instance variable (more or less like object properties) should have its access method (one to assign a value to it, and one to read the value from that instance variable).

Squeak has a preference that allows automatic generation, when required, of the access methods.

From the menu World - appearance - preferences - type (in the textbox with red colored font) autoacc, and press Enter. Then, select the Autoaccessors characteristic.

After doing this, Squeak will ask to create each required accesor, the first time an object needs to be read or saved in an instance variable.

Obviously, Squeak creates accessors in a generic way; if the programmer needs more specialized code, he should overwrite the code generated by Squeak.

The accessors created, for instance, for url, are:

url
   ^ url 

This is the read accessor, ^ is the symbol that indicates that it should return the object whose name follows, in this case, url.

url: anObject 
   url := anObject

In the first line, the colon means that it receives another object (in this case, anObject) as a parameter, in the second, := is the assignation symbol, which means that url is assigned anObject, therefore, url = anObject. Sometimes, instead of :=, <- is used.

Some modified accessors:

parsedPage: anObject
parsedPage := HtmlParser parse: anObject

The last statement means that parsedPage obtains its value as the result of the expression to the right of the :=, that is, HtmlParser parse: anObject.

The method parse: of HtmlParser receives anObject (the object which we want to parse), then, the content of parsedPage is the result of applying the parse: method of the HtmlParser object to the object which we pass (in this case, the Web page).

HtmlParser is an object that also lives in the Squeak image. Opening the image is like opening the old trunk of our grandmother, which is full of strange and interesting things. Thus, within a Squeak image, we find the compiler, the debugger, an Internet browser, an e-mail client, scripting, tools to work with 3D objects, objects for connectivity and networking, etc. ... thousands of things, ready to be used.

urlPage: anObject
urlPage := HTTPSocket httpGetDocument: anObject

Here, the HTTPSocket httpGetDocument: anObject obtains the object anObject from the Web, and assigns it to a urlpage.

HTTPSocket, too, is an object that lives in the Squeak image. All objects that live in the image can obviously be modified or used to create subclasses of themselves (inheritance).

Try to write the methods in the lower half of the browser and accept them, as shown here:

   Page 33 of 64 

 

 

October 2003
   Page 34 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


Trying the code out!

Open a workspace (from the World menu, or pressing Alt+k) and type:

x := WebReader new. 

Evaluate it with ald+d, alt+p, or from the menu. This will create a new instance of the WebReader called x.

Now, evaluate the following:

x url: 'http://localhost'.

As an object that inherits from WebReader, x has the same instance variables and methods as WebReader; then, we are assigning 'http://localhost' to the instance variable url. For the exercise, you must have a localhost with some Web server running on your machine.

Now, evaluate:

x urlPage: x url.

Retrieve the page accessed by url. Now, evalute:

x parsedPage: x urlPage.

Here, we are passing to parsedPage: the return value of urlPage (remember that urlPage returns itself, in this case, the page retrieved in the previous step).

Now, evaluate the workspace, with Print It or Alt+p:

x parsedPage contents.
   Page 34 of 64 

 

 

October 2003
   Page 35 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


The previous steps will show us the retrieved page, as an OrderedCollection (a type of collection which any Smalltalk, in this case, Squeak, handles). We can also try out some other very useful Squeak tools:

x parsedPage explore.
x parsedPage inspect.

Both are used to see objects and their contents.

Next, let's examine the OrderedCollection seeking the title tag - we will not include the code here. Several different things can happen in this case:

  1. The page has no title tag; in this case, our program should do something (evaluate the following in the Workspace):
    Speaker manWithHead say: 'The page has no title.'.
    or:
    Speaker manWithHead say: 'This page doesn't mention Squeak'.
    

    Speaker needs the sound to work correctly in your computer.

  2. The page has a title tag:

    In this case, we must examine this tag searching for the Squeak word; we won't include the code for this here, either. If it is found, we can recover the complete title tag and save it in some variable, for instance, xTitle.

    For testing purposes, let's assume that the title tag is something similar to this: 'Squeak is the Smalltalk of the 21st. century'. In this case, we must evaluate:

    xTitle := 'Squeak is the Smalltalk of the Twenty-First Century'.
    

    and then:

    Speaker manWithHead say: xTitle.
    

    If the title tag doesn't contain the word Squeak, our program will do the following:

    Speaker manWithHead say:
     'This page is not related to Squeak'.
    

Not many languages and development environments allow us to do this sort of things as easily as Squeak, and using real object technology (not only object orientation).

This application can be completed developing a GUI, or saving objects within the image (creating a small knowledgebase about visited urls and their titles), etc.

All this can be developed using the same style of work with objects.

   Page 35 of 64 

 

 

October 2003
   Page 36 of 64  
 


 

... Squeak, the Smalltalk of the 21st. Century


¿What other applications exist in Squeak?

Today, Squeak is a product that has a very active community, which takes care both of maintaining and evolving the environment, and of developing diverse additional products in Squeak. An easy way to see the amount of software available for use in Squeak is from the World menu, open - Package Loader. This presents us this utility which allows us to install, update and uninstall packages. Here, we can see that there are Web servers, a framework for develping web applications, a VNC client, multiple appications for manipulation of graphics and multimedia, cryptography, a DNS client, an IRC client, a framework for mapping objects to relational, a MAPI client, an object base, ODBC, a MySQL client, a PostgreSQL client, a PDF reader and a long "etcetera".

On the other hand, Squeak runs on a variety of platforms, including PDAs, and it is possible to use it to develop a wide variety of applications, for instance: business applications, web applications, multimedia, embedded applications, hardware and device applications; all of this, using objects, which gives us a distinct advantage when we have to maintain and evolve the software.

In consequence of one of the design principles of Smalltalk expressed by Dan Ingalls many years ago, which says that the operating system is something that should not exist, there is also a project called SqueakNOS (in SourceForge) to develop what has been called Squeak No Operating System, that is, a Squeak that directly controls the hardware on which it runs, without the need of an operating system.

Also, and to give one example more, Squeak can be used as a windows manager of any Linux, replacing Gnome, KDE, or whatever, using some of the modules that can be loaded with the Package Loader. Surprising, isn't it?

Some interesting URLs:

  • Official Squeak site
  • Official Squeak Swiki
  • Squeakland
  • Spanish Squeak project, applied to education
  • Argentinian group dedicated to Smalltalk and Object Technology

Other Smalltalks:

  • Dolphin (Only Windows - Commercial)
  • Smalltalk MT (Only Windows - Commercial)
  • Visual Age for Smalltalk (Multiplataforma - Comercial)
  • Visual Works (Multiplatform - Commercial)
  • Smalltalk/X (Multiplatform - Free)
  • Smalltalk Express
  • Visual Smalltalk
  • Gemstone Smalltalk (Object base)

Conclusion

This article intends to give a brief and concrete introduction to the world of objects and Smalltalk, especially Squeak. If after reading it, the reader has curiosity of further investigating Smalltalk, I will have fulfilled my objective in writing this.

 

   Page 36 of 64