This is the first article in a series, to read the whole series go here.
You’ve heard about it, every child will have a portable computer, small enough to carry in her bookbag. Its software will be an open environment that they can change and program themselves. I’m talking, of course, about the Dynabook, from Alan Kay’s proposal in 1972. The programming language that was to be the center of the Dynabook and its environment was Smalltalk, one of the first object oriented programming languages. Smalltalk was intended to be easy for children to learn, to that end has a simple and highly regular syntax and a deeply interactive environment.
Dynabook was an idea before its time. It lives on in spirit in the OLPC project, which also contains Smalltalk, or an application of it in the form of Squeak’s EToys.
Modernly Smalltalk has a number of implementations. One of the best known is Squeak which Alan Kay continues to be involved with. Squeak is an wonderful programmer’s playground where Kay and his team have tried out a multitude of ideas, still with the goal of creating an environment that children may learn in. Unfortunately for the working programmer the playground has become rather cluttered with toys, so the Pharo team has made a stripped down version for a much leaner programming environment.
As I alluded to above, Smalltalk isn’t just a programming language, it’s an environment in its own right. When you open up the Pharo image you have an entire integrated development system, including the System Browser where you do most of your coding, the debugger and the unit test system. And all of which is written in Smalltalk and can be modified inside the system in real time.
What to Expect
When this article is complete:
- You will have:
- An installation of Pharo
- An object that says Hello
- You will know:
- Basic elements of the Pharo/Smalltalk environment including:
- The Workspace
- The Transcript
- The Class Browser
- How to create new classes in the Pharo environment
- Basic elements of the Pharo/Smalltalk environment including:
What You’ll Need
To install Pharo, start by downloading the zip of latest version from Pharo Downloads. You’ll have the choice between the Cog and Standard VM. Cog is newer and faster, which won’t matter much for our purposes, but why not get the fancier toy when you can?
Unzip “Pharo.app” to the location you want to keep it, “Program Files” will do the job. Once you’ve unzipped it, navigate into the Pharo.app directory and double click the “Pharo” link and your new Pharo system will open.
A Brief Tour of the Environment
Smalltalk was designed to be an entire environment, separate from whatever system that it’s running on. The advantage is it gives you a completely portable programming environment that you can move to any machine that has a Smalltalk VM and it will work the same way on each of them. A disadvantage is that there’s a lot to get used to in it. For the next couple of weeks, 90% of the work is going to be in the System Browser with side trips in a few of the other tools. The major components of it are:
- System Browser: This is where you’ll do your actual coding.
- Packages: Packages are similar to modules, but they only have formal existance when you’re importing them in the system. Once they’re part of your Smalltalk image, their purpose is purely organizational.
- Classes: Once you’ve selected a package, the classes are the primary element in the system.
- Protocols: Protocols, like packages, are a way to organize the methods in a class.
- Methods: Methods are what the classes do.
- Edit Pane: The Edit Pane is where you’ll do most of your coding including specifying classes and adding methods.
- Workspace: You can view the Workspace as a scratch pad, you’ll use it when you’re trying to figure something out in the small, or when you’ve completed a piece of work and are doing a bit of ad hoc testing.
- Transcript: The transcript is where you’ll print logs and quick bits of system interaction. You can think of it as a combination of stderr and stdout in a more conventional system.
Files Used in this Project
- Virtuous-Demo.st: Contains all of the sample code developed during this project.
To import “Virtuous-Demo.st” into your Smalltalk image:
- Open Pharo
- Click on the background to open the World menu.
- In the World menu select Tools > File Browser
- In the File Browser dialog, navigate to the folder that contains “Virtuous-Demo.st”
- Select “Virtuous-Demo.st”
- Click the “install” button at the top of the File Browser dialog.
- Close the File Browser.
You can verify that the package has been correctly installed by doing the following:
- In the World menu select System Browser
- In the Package pane, scroll to the bottom where you should see a package called “Virtuous-Demo” which contains the class “Greeter”.
To keep it as part of the image (along with any other changes) click “Save” in the World menu or to exit click “Save and quit”.
To execute code in Pharo:
- In the World menu, select Workspace, this is where you’ll enter the code to execute.
- In the World menu, select “Tools” > “Transcript”, this is where results will be displayed.
In the Workspace window enter the following code:
Transcript show: 'Hello World'; cr.
There are two ways to run the code. You can either right-click on the same line as the code and select “do it” or fellow keyboard addicts can type Alt-d while the cursor is on the same line as the code. In either case the words “Hello World” will then appear in the Transcript followed by a new-line. You might also see some paired numbers that come up during the process, this is a side effect of the Transcript being where logging data for the system in general is sent.
Understanding the code you just Executed
One element at a time:
Transcriptis the object of interest, generally objects will start with a lowercase letter, but this is a special case
show:is a method (message) that is called (sent) to the
Transcriptobject. This says that whatever object it is given should be written to the Transcript window, the
:indicates that it takes an argument which is…
'Hello World'is a string, note that Smalltalk strings are surrounded by single, not double quotes.
;indicates the end of one method, but that the following method is also getting sent to the same object.
cranother method that is sent to Transcript and writes a new line to the Transcript window.
.indicates the end of a statement.
Coding in Smalltalk
The first step you’ll take in your Smalltalk project is to add it to the Packages pane of the System Browser window. To do this:
- Make sure that the Packages pane is selected, left-clicking it will do the trick.
- Right-click in the Packages pane and select “add category…”
- Enter a name that works for you and click OK. (Mine was “Virtuous-Demo”, remember, this is just to keep your code organized)
Now that you’ve added your category, select it from the Packages pane, notice that in the Edit Pane there’s a template for creating a class:
Object subclass: #NameOfSubclass instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Virtuous-Demo'
You’ll want to add a new class:
- Change ‘#NameOfSubclass’ to ‘#Greeter’
- Right click in the Edit pane and select “accept” (or type alt-s) to write your class to the system.
Now you can see the new class in the Classes pane. In the Protocols pane click “as yet unclassified”, this is a generic bucket for any methods you write without sorting them. Now you can see a template for a method in the Edit pane:
messageSelectorAndArgumentNames "comment stating purpose of message" | temporary variable names | statements
Start writing your method, since there’s going to be no need to track temporary data within an object I made this a “class” rather than “instance” method, so click the “class” button underneath the class pane. Now change the Edit pane like so:
sayHello "Greet the User" Transcript show: 'Hello World'; cr.
The first line is the method name, followed by a comment. I removed the
| temporary variable names| because there was no need for them, and put the actual action of the method in the last line.
When you’ve finished entering the method “accept” it the same way you did with the class. When you “accept” a new method for the first time, it will ask you to enter your full name. Smalltalk automatically tracks all changes to the system and it will uses each individual’s name to track who has made the changes.
To try your new method out, type
Greeter sayHello. into the Workspace and then execute it.
Arguments in Methods
To get an idea of how arguments, I wrote a version of
sayHello that knows who it’s talking to. To add another method click “as yet unclassified” in the Protocols pane again and the Edit pane will be populated with the template again, add the method
sayHello: as below:
sayHello: toWho "Greet the User" Transcript show: ('Hello ', toWho); cr.
Inside the method declaration is the argument list. To indicate that a method has an argument append a
: to it and follow it with the argument name. In the body of the method there’s one more bit of Smalltalk syntax the
, operator concatenates two strings. Strictly speaking it’s not Smalltalk syntax, but just an operator attached to the String class. I’ll get into the details of operator declaration in a later article.
Try this method by typing
Greeter sayHello: 'Professor'.
Nice things about Smalltalk so far:
- Very simple syntax
- Very clear code
Difficult things about Smalltalk:
- Learning the environment
- Resisting the urge to spend hours just playing with the system instead of working
Next week I’ll be putting together a class for calculating the “FizzBuzz” problem and unit test it using Pharo’s integrated unit test system.
- Squeak: Squeak Smalltalk is the basis of many of the modern free Smalltalk implementations
- Pharo: Pharo is a professional design system based on Squeak
- Smalltalk Zen: Dimitri Zagidulin’s informative page about web development in Pharo/Squeak
- Smalltalk Language Notes: A nice reference for a lot of basic Smalltalk programming