Squeak? What does this have to do with iPhone programming? A significant amount.
Squeak is a modern implementation of the Smalltalk-80 language. It inherits directly from the project initiated by Alan Kay at Xerox PARC. The project was originally supported by Apple and was used heavily by Disney. For a very long time, the speed at which one could build an app in Smalltalk really could not be compared. Unlike programming in Objective-C where you deal with text files, Squeak is an image based development environment. Squeak is in fact very much like it’s own operating system. This has it downsides in that for a long time it was difficult to talk outside to the hosting environment to access functionality that didn’t exist in Squeak. For many tough projects this really doesn’t matter, but it has limited its popularity outside of a very deep and dedicated programming community. Thus, Smalltalk is not nearly as popular as it once was. However, after this tutorial you should see that it supports features that really put most other programming environments to shame. It will also highlight the concept of messaging that programmers learning Objective-C find so strange.
The important thing to take away is that Squeak is one of the very first Object Oriented (OO) programming languages. All other OO languages (C++, Java) draw heavily upon it, whether directly or indirectly. Unfortunately most of the interesting good ideas got left out. Objective-C in fact does better than C++ and Java in staying true to Smalltalk’s design. This because Objective-C fundamentally works via messaging. Messaging is as important today as it was then, in fact many programs that involve a great deal of concurrency operate via messaging (think Erlang). Perhaps you’ve heard that Objective-C is Smalltalk+C, you’re about to see that connection much more clearly.
First let’s install Squeak. Goto the Squeak site, on the right nav there’s a download link for Macs.
Once you downloaded and unzipped the file. Double-click the Squeak application to launch it. Once launched your screen should look something like the following.
The Squeak environment is very unlike what you will be used to. The entire environment is live so to speak. When you are programming in Squeak, you don’t need to manage text files, you’re actually writing code directly into the system and it gets compiled immediately. Gone is the tedious compile-link-run cycle. Though, we’re not going to get that far, we’re going to see some interesting things here.
First right-click on the Welcome too… window, you’ll get a bunch of things around the window called halos. These do several things but just for fun, lets try the on in the lower left corner, Rotate. Click and drag this halo. Your window should rotate.
Try clicking inside the window and typing some text. It works perfectly. A great example of the power of an object-oriented system that has “turtles all the way down.” Click the window’s close box.
Create a workspace by dragging one onto the desktop area. If you click on the Tools flap on the right side you will see a little box called Workspace. Click and drag this onto the main area. You’ve just created an instance of the Workspace class. This is Squeak’s version of Terminal.app.
Let’s type something into this workspace. Type the number 9. This is not just the number 9 as we shall see shortly. It is an Object. Don’t believe me? Type the word class just right after the number 9 on the same line (make sure there is at least one space between 9 and class). Make sure that your cursor is at the end of the line and type Command-P. This is the short cut for Print It.
This tells the environment that you want to run the current line as a peice of Smalltalk code and print the output on the same line. You have just sent the object 9 a message. The message was class.
Let try sending 9 another message. Skip down to the next line type 9 again followed by the message asFloat and again type Command-P making sure that your cursor is at the end of the current line of text. You should see 9.0.
Again drop down to the next line and type ’string’ class, be careful that you use single quotes. SmallTalk uses double quotes to represent code comments. Print out of the output of sending the message class to the object ’string’. You should see ByteString as a result. Select the text ByteString, and type Command-B (for Browse It). This will open up a System Browser.
Wow’s that a little overwhelming. This is in fact what it looks like. It is a window into the entire running System. There are four vertical panes in the top half of the window. The leftmost shows categories of classes. We’re currently looking at Collections-Strings. Inside of a category you have actual classes, like ByteString. This is the second pane. The third pane organizes methods into categories. The last pane contains the actual methods. Method are implementations of the messages you might send an object.
Something that is common, especially if you come from microcontroller programming is converted characters to and from their ASCII character code. Let’s close the System Browser and go back to our work space. Double-click on the text SmallInteger and Browse It.
A new System Browser should open up. In the third pane select the converting category of methods. Scroll through it, hmm, there’s no method describing converting a number object into a character or string. Perhaps it implemented in a superclass. We can see the class hierarchy by clicking the hierarchy button the bottom half of the System Browser. SmallInteger inherits from Integer. Click on Integer and then click on the converting category of methods. Looks like there is a method called asCharacter.
Close the System Browser, on a new line in your workspace type 65 and send it the message asCharacter. Print It. You should get the character $A as expected.
There’s a lot of great OO style programming in Squeak, it’s a veritable resource of fantastic ideas. And one of it’s best concepts, blocks, are finally making their way into C and C++. You will be hearing a lot about blocks in the future.
When you are programming code for the iPhone you will be doing pretty much the same thing you did just now, it’ll just be a little less convenient and elegant.
1. You will send messages to objects.
2. You will look up methods (implementations of messages an object can receive).
3. You will look up classes, classes couple data with functionality.
4. You will dynamically send messages to objects while debugging (interact with live objects).
We didn’t actually define a new class, but we’ll get to that soon enough in Objective-C. If you click on a blank area of the work space you will get a menu. Towards the bottom there is a option Save and Quit. Zip up and send me your image file.