Smalltalk for Everyone Else
Pages: 1, 2, 3
Now use the browser to add a few methods to your CronGen class. First, middle-click (or, on Windows with a default configuration, right-click) in the class category pane of the browser. Select find class... and enter "CronGen" in the resulting dialog. Don't worry about method categories for now. Instead, select -- all -- as the category. The code pane at the bottom of the browser will display a template reminding you of the syntax of the method. Now add accessors for your command instance variable:
command
^command
command: cmdString
command := cmdString
Note that the two new messages both have command as a selector. One is a unary message that simply returns the command instance variable, while the other is a keyword message that accepts a cmdString, which it assigns to the command instance variable. This is a fairly typical syntactical pattern when adding setter/getter methods to a Smalltalk object.
Now create some class-side methods. To do so, select class at the bottom of the class pane. Add the myDayMapInit from earlier in this article. Also add an accessor for the map that treats it as a singleton, initializing it if necessary and returning it:
DayMap
DayMap ifNil: [ DayMap := self myDayMapInit].
^DayMap.
The browser isn't the only development tool in the Squeak image. Evaluate this code in a workspace:
CronGen DayMap inspect.

Figure 3. Using the Smalltalk inspector
This will open an inspector on the DayMap singleton (Figure 3). In this case, because the map is a dictionary, a DictionaryInspector is opened. Inspectors allow you to examine the values associated with a given instance. The dictionary inspector allows you to look at the member variables of the dictionary DayMap and to look at the elements that it contains.
You can see that the tight integration of the language, the class library, and the development tools facilitate a unique development style. To manipulate and inspect the objects of your application, simply send messages to your object instances. Perhaps the most important of these messages is the halt message. Kent Beck, the father of eXtreme Programming, has reportedly advised "Don't think, just set the halt," allowing you to inspect the live system and fix defects.

Figure 4. The walkback from a halt
Imagine that your application that uses the CronGen class is having a problem where some of the CronGen instances have their command set to a number rather than a string. Edit the command: method to add a line to the beginning of the method:
(cmdString isKindOf: Integer) ifTrue: [ self halt. ].
This will trigger a walkback at the point where the halt is sent (Figure 4). From the walkback, it's possible to drop into a debugger, from which you can inspect the state of your program (Figure 5).

Figure 5. The Smalltalk debugger in action
To see this in action, evaluate this code in the Workspace, one line at a time, after having made the previous change to your image:
cg := CronGen new.
cg command 1.
Squeak also provides extensive facilities for navigating pre-existing code. This helps both with understanding the services available in a base image and to come to an understanding of locally developed legacy code. In a running Squeak image, click on the tab on the right border of the main Squeak window. Drag the icon for Method Finder to the desktop.

Figure 6. The Smalltalk method browser
The Method Finder does just that. Type a method-name fragment in the upper right window, and you'll receive a list of candidate method names (Figure 6). Selecting one of those lists the classes that implement the selected method, which you can then browse. The System Browser provides similar services with context menus that allow you to browse senders and implementers of any selected method.
Smalltalk's long history ensures that there is a great quantity of information available to you for further study. The Squeak.org website provides a central point of information about the Squeak implementation of Smalltalk, while Smalltalk.org serves a similar purpose for the language in general. Stéphane Ducasse, a prominent Squeak community member, has provided a great community service by petitioning the rights holders of several out-of-print Smalltalk books to allow him to post online versions of Smalltalk books. The Squeak mailing lists are populated with an extremely friendly, knowledgeable, and helpful community that resemble the language they work with: broad and deep and repaying manyfold the effort you put in.
Keith Fieldhouse is a software developer and writer living in upstate New York with his wife and two young daughters.
Return to ONLamp.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 17 of 17.
-
The Pragmatic Programmer
2006-11-28 21:53:45 jeberle [Reply | View]
-
The Pragmatic Programmer
2007-01-25 20:05:53 jacaetevha [Reply | View]
That's funny. Running Squeak on my MacBook Pro (1.83 GHz Intel Core Duo, 1GB 667 MHz DDR2 SDRAM)Time millisecondsToRun: [2 raisedTo: 64]produces the right answer and takes less than a millisecond. To be fair, that's not printing the statement like you did. So after adding a print statement in there, it takes 18 or 19 milliseconds. Your Ruby code takes 45 milliseconds.
-
never too late for smalltalk
2006-10-22 11:52:13 SecondTry [Reply | View]
My comment (lengthy) appears to have gotten lost by the server, so I'll try to re-frame my thoughts.
Smalltalk was strong because it abstracted away the OS, leading to pure OO and VMs. However, to do this, it used a framework based around image files rather than text source files. Many Smalltalkers don't use vi/emacs regularly anymore, and can't relate to the lost productivity they're asking other people to suffer through if migrating to Smalltalk. It's simply unacceptable, in my view, to drop your toolkit, unless there's a good reason. Granted, versions like GNU Smalltalk are file based.
However, Smalltalk has tremendous strengths. Despite what the UML-Java gurus tell you about OO, chances are that none of them has worked in a real OO environment. To see the poetry in motion, you have to consider looking at something like Smalltalk. There are also a variety of new languages like Ruby and PHP5 which offer more in the way of pure OO than Java does.
-
There is a lot more than Squeak
2006-09-29 17:39:22 eMaringolo [Reply | View]
Beign Squeak one of the most vanguardist, revolutionary, open source and active Smalltalk dialect, there exists some dialects which can fit best to other software developers. I recommend Dolphin Smalltalk, you can download it's free community edition from their website at www.object-arts.com.
It's a one way trip.
-
Its a lot to get used to
2006-09-24 19:28:30 tlaurenzo1 [Reply | View]
Having recently tried to get started with Squeak, I can say that it is hard for someone used to text editors, interpreters, and compilers to get used to the typical Smalltalk environment such as Squeak.
I get Smalltalk the language, but I don't get Squeak (which from what I understand is fairly typical as a Smalltalk environment). It'd be nice if there was some kind of guide like "Squeak and Smalltalk for those addicted to their text editors and command prompts." From some comments I see around, I think that I am not the only one with this mental block.
Good article nonetheless. -
Its a lot to get used to
2007-01-25 20:09:04 jacaetevha [Reply | View]
No, Squeak is not fairly typical. You can however use emacs key bindings or the wonderful svi package so that the browsers feel more like the powerful text editors you are used to.
The point about the image though is not so much that you lose those things, it's that the productivity gain you experience FAR OUTWEIGHS what you "lose". -
...but it shouldn't be.
2006-10-02 06:28:50 cbmanica [Reply | View]
I don't see why those of us who use highly capable text editors and command prompts should be forced to view proficiency and comfort with Emacs as a "mental block". The fact that Squeak (and apparently other Smalltalk environments as well) tie you to a text editor only marginally more capable and useful than Notepad is only one of many unnecessary absurdities one must accept if one wishes to explore the Smalltalk design paradigm. -
Re: Its a lot to get used to
2006-09-28 10:49:44 whartung [Reply | View]
Yes, it's the classic language vs library syndrome. For all of the simplicity, dynamism, and exploratory nature of Squeak and Smalltalk, the key is mastering not just the language, but also the class library. And the class library is quite daunting.
For someone who's used to dealing with files filled with lines of code, learning a system 10 lines at a time through a browser window in a disjointed way can be quite difficult.
-
Have you tried the powerful tools available
2007-01-25 20:16:10 jacaetevha [Reply | View]
In Squeak (not sure if other Smalltalks have something like it) there is the MethodFinder and the MessageNames dialog that help you learn/discover the system in wonderful ways. After finding the MethodFinder I can't hardly stand not to use it. It takes a receiver, an argument (or list of args), and an expected result and gives you what methods in the system would produce that result.
Eg.
MethodFinder methodFor: #((2 64)18446744073709551616. ).
yields:
'(data1 raisedTo: data2) (data1 raisedToInteger: data2) '
-
Other language with image
2006-09-23 07:05:45 lvaruzza [Reply | View]
The author says:
"The image, however, doesn't have an analogue with other languages--it is a platform-independent snapshot of the current state of the VM's object memory."
But Common Lisp has a similar concept of image. See the reference for SBCL:
http://www.sbcl.org/manual/Saving-a-Core-Image.html
-
Other language with image
2006-09-23 14:36:42 wanorris [Reply | View]
Is that a part of the common lisp standard, or is it specific to SBCL?
Either way it's cool, I was just wondering. -
Re: Other language with image
2006-09-28 10:58:51 whartung [Reply | View]
This is a common meme of all of the major Common Lisps. Emacs is a bit of an exception here, looking more like the perl/python/java model of having a bunch of discrete compiled modules that are registered with the system and loaded as necessary.
But the image nature of both CL and Smalltalk is very important, and quite different from the "rebuild it all from scratch" methodology of most programmers. With the image, you essentially have not only your program, but the entire system loaded and available to tweak and change.
For example, unlike, say, Java, in Smalltalk if you want to add a new method to a system class (say, oh, Object or String), you can easily just browse over and add a method. Voila, instantly every object or string in the system has that new message. No compiling or building or relinking or reloading. It just Is.
Obviously, this can lead to trouble if you do something silly -- with great power come great responsibility, but Smalltalks change management mechanism helps mitigate such disasters.
One could also argue that many Forth systems are image systems, they just tend to be much much smaller than ST or CL.
-
Re: Other language with image
2006-09-29 10:28:23 prand [Reply | View]
Lisp has images, but as far as I can tell, most lisps haven't created a self-contained, graphical, introspective IDE in the style of smalltalk. I've wondered why.
For one thing, graphics and GUIs are add-ons to lisp, rather than built-in like smalltalk.
Also, most lispers seem to be very adept at Emacs, which is very powerful, but hard to learn well, and very keyboard-intensive.
I've had daydreams about creating a smalltalk-style environment for lisp, either by implementing a lisp in smalltalk, or by recreating a lisp version of the smalltalk libraries and frameworks. Sounds like a LOT of work, but what fun!







Note, on OS X, you can get this answer out of the box.