Help - Programming - Programming in Smalltalk and Squeak

Previous - Programming - Next

Introduction

Programming in Squeak must start with considering programming in Smalltalk, for Squeak is a dialect of Smalltalk - it is Smalltalk-80 plus Morphic, in a formula. Here are first some web-urls for background on Squeak and Smalltalk

Dwight Hughes' website with classic papers of Smalltalk and Squeak
(
http://users.ipa.net/~dwighth/)

Free books on the website of prof.dr. Stéphane Ducasse at Berne University, Switzerland.
(http://www.iam.unibe.ch/~ducasse/WebPages/FreeBooks.html)

There are quite a few implementations of Smalltalk, and Stéphane Ducasse gives a nice collection of pdf-files that are helpful with almost all dialects. Personally, I like "The Art and Science of Smalltalk" by Simon Lewis and "Inside Smalltalk" by Wilf Lalonde and John Pugh best, but there is plenty of choice.

If you don't know Smalltalk and want to learn to write programs in Smalltalk or Squeak, this is the place to go first and select a few books and start reading!

The difference between Squeak-as-is ("a standard out of the box release" since Squeak 3.0) starts as a Morphic Project. So what you see and what you work with in Squeak (unless you work in an MVC-Project) looks different from and has other capacities than what works in other Smalltalks, for they lack Morphic and don't have any Morphs.

The basic idea of programming in Smalltalk

In Smalltalk, programming is radically simplified, by letting everything that runs inside the environment be an object, in a special sense of that word.

An object - in Smaltalk - is a package consisting from data and methods to work with the data, and is itself a simplification of what a computer is - a machine with methods to store and and change information systematically.

Once an object is running inside a Smalltalk environment, its data can only be accessed by its methods, and its methods can only be worked by sending the object a message. (See: encapsulation.) A message is a string which names the object followed by a name for one of its methods, followed possibly by parameters.

Note this is like a computer: its data can only be accessed by its programs, and its programs must be started by an appropriate string including possibly parameters.

So an object in Smalltalk is like a little computer inside the big computer, and fit to contain and work with information methodically, when instructed by an appropriate message. An appropriate message always has the following form: The name of an object, followed by a name of the method, followed possibly by parameters, that are indicated by words ending on a colon. In general it looks like this, then: objectname method: firstParameter with: secondParameter.

Here is a simple example how this works inside Smalltalk.

The Transcript is an object in any Smalltalk environment, and is used by the system and the user to display information. You can open one with Cmd-t, with the cursor in an Project window. Do so.

Now consider the next line

Transcript show: 'Hello, this is Smalltalk!'.

Here Transcript names the object, show: names one of its methods, followed by a colon to indicate a parameter follows, followed by a parameter in the form of a string, namely Hello, this is Smalltalk! between single quotes, which is Smalltalk's way of marking the beginning and end of a string.

If you highlight the whole magenta line and do Cmd-d (Alt-d), you'll see in the Transcipt the message:

Hello, this is Smalltalk!

Here is another example. Select the following line and do Cmd-p to print the result.

12345 squared + 6789 squared

    This works because numbers too are construed as objects in Smalltalk, and both squared and + are methods that numbers in Smalltalk have. And it works because in fact the message was sent to an object called the Interpreter, that handles the messages a Squeak system gets - which itself is a collection of running objects.

And here is a combination of the examples

| calculation |
calculation := 12345 squared + 6789 squared.
"Does the calcution and assigns the result to calculation"
Transcript show: calculation.
"Shows calculation i.e. the value just calculated in the Transcript"

If you select the three lines and do Cmd-d, you make the variable calculation, which gets assigned the result of the calculation, after which the Transcript is told to show: that calculation.

In fact, you now have the main ideas on which Smalltalk is based, but it helps to repeat the initial claim and explain a little: Everything that runs inside a Smalltalk environment is an object, including the Smalltalk environment.

Hence programming in a Smalltalk environment is the writing of objects, their methods, and their data-slots, and then using these by sending them messages that name one of their methods and perhaps also send it parameters.

Also, objects can send messages to objects, and can be part of other objects and contain other objects as parts. And each object in fact is a package which may contain many methods (a.k.a. programs, procedures, functions) to change a lot of its data. So figuratively, an object is a bundle made up of programs (called methods in Smalltalk) and data that can only be altered by the programs in the bundle, of which there may be many.

Finally, in Smalltalk objects are grouped in a hierarchy, of which the top is a class called Object, and such that a kind of object A that is a sub-class of another kind object B also can call on the methods of the class B it is contained in - and so on upwards to Object, for every object in a Smalltalk environment is an instance of Object. Thus every object in a hierarchy of objects has automatic access to the methods of the objects in its hierarchy that are higher up in the hierarchy.

This relation of containment between one class (a sub-class) and another (a super-class) that involves that a sub-class may call on its own methods and on the methods of its superclass(es), is called inheritance.

The basic idea of programming in Squeak

In Squeak, one programs in Smalltalk-80 that is extended with the category of classes called Morphic that contains Morphs. A Morph is a part of the screen that works as an object in Squeak, and therefore can be send messages.

Here are a few short lines that restate the above section about programming in Smalltalk and add what morphs are. The "acts as" terminology is meant to refer to the main idea something is - supposedly - implementing:

A morph acts as interface of an object.
An object acts as a software computer, that consists of a set of dataslots and a set of programs.
A message acts as a name of an object, a name of a program and possibly (a name of) data.
A program acts as a series of instructions to a processor where each instruction is a calculable function.

A calculable function is a relation between a sequence of inputs and an output such that the output is uniquely determined by the sequence of inputs (this is why it is called "function": one definite output for any given input) that can be found from the inputs by a finite series of mechanical transformations. (If you now want to know what mechanical transformations are, think of simple mathematical functions like adding or multiplying or consult Marvin Minsky's "Finite and Infinite Machines".)

The two main ideas you have to get (and the getting will take time, just as getting a natural language takes time) seem to be

(1) Everything that can be programmed in Squeak is programmed as an object - as a software computer that can do a series of specific tasks, namely those for which it has methods. Consequently, in Squeak one has objects in objects in objects calling on objects that call on objects...
(2) Everything that can be programmed in Squeak is programmed in the Squeak environment, which itself is programmed in Squeak. See Total Computing.

Your first help in programming quandaries:

A. Helpful Important keyboard shortcuts

What is the Cmd (Command) key on Apple is the Alt key on Windows and Linux. There are many important keyboard shortcuts in Squeak.
Here are a few basic ones - and all do their stuff on highlighted magenta parts of text, nearly anywhere inside Squeak:

Cmd-d = Do It = process the text. Browser openBrowser
Cmd-p = Print It = Do It and print the result behind it. 5+4+3+2+1
Cmd-b = Browse It = start a browser on the object with the highlighted name. Project
Cmd-i = Inspect It = Do It and start an inspector on the result. #('One' 'Two' 'Three' 'Go') asSet.
Cmd-m = Display a collection of implementors of the term. collect:
Cmd-n = Display a collection of senders of the term. do:

Cmd-k = Start a new workspace (when the cursor is in the Project window, not on some morph inside it)
Cmd-t = Start a transcript (when the cursor is in the Project window, not on some morph inside it)

Cmd-. = Stop it = Cmd key followed by a dot ends whatever activity Squeak is busy with and returns to the state that was before that activity started. (Use this when you think something is wrong or takes too long.)

There are many more command-keys, notably in a Workspace. Here is a list of those that work in a workspace:

Utilities openCommandKeyHelp. "Select and Cmd-d pops them up. Also in Worldmenu-help."

B. Helpful Browsers

Browsers in Squeak are Objects written in Squeak that allow the human user to see the code and internal states of Squeak as it runs. There are many Browsers in Squeak. See BrowsingSqueak. Here is code for opening some:

Browser openBrowser. "opens the System Browser"
HierarchyBrowser newFor: TextPlusMorph. "opens the Hierarchy Browser on TextPlusMorph"
MessageNames openMessageNames. "opens the Message Names browser"
SelectorBrowser new open. "opens Method Finder"
ProtocolBrowser openFullProtocolForClass: ProtocolBrowser. "Opens a Protocol Browser on its own protocol."

If you select the above five lines Cmd-d will put all five on your screen. Their upperleft x removes them.

Alternatively, if you know the name of a class (which is a factory for objects of that class), you can get a System Browser on it by 'fullOnClass'. For example:

Browser fullOnClass: Browser.
Browser fullOnClass: Array.

Previous - Programming - Next