«« What's Up, Doc? UI Candy »»
blog header image
A Toe in Smalltalk Lake

On ambrai's web site I found a list of free Smalltalk books and I've been reading The Art and Science of Smalltalk. It starts off with no assumption of OO programming experience, and is old enough (1995) that it doesn't even mention Java, just C and C++.

There are some interesting things about Smalltalk I've picked up from this book so far (please correct me if I go astray):

Everything is an Object

Everything in Smalltalk is an object. Unlike Java, there are no "primitive types" for performance.

Variables Don't Have Type

Variables, just as in Java, point to instances. Rather than saying that variables are "pointers", like in C or C++ most people say that a variable is a reference to an instance. Like Java, instances are passed to methods by reference.

In Java and Smalltalk the "type" is the class that the instance comes from. The difference between Java and Smalltalk is that Java enforces types -- it makes sure that the type of the variable is the same as the type of the instance, or that the variable's type is a superclass or interface of that instance.

Smalltalk does not enforce types and any variable may point to any type of instance.

Messages

Classes and instances still have methods, but you don't "call" them like C, C++ or Java. Instead you send the instance a message. If the instance has a method that understands the message, it's dealt with by that method. Otherwise the message is ignored.

This ties in with the lack of variable types. Let's say instance A sends a message X to instance B. A might assume that B is of a certain type and will understand the message, but if B doesn't understand X it will be ignored. In Java, trying to call a method that didn't exist in an instance wouldn't even compile.

At first glance, this seemed dangerous to me. But the book says it's part of the power of Smalltalk, so I'll wait to be convinced. I could see it being handy for refactoring, where in Java it's a lot harder to refactor from object 1 to object 2 because you have to correct all of the methods to even make it compile. That type-checking might save your ass, but it's also a pain because it's so restrictive. With good unit testing, this issue could go away.

Encapsulation is Enforced

There's no such thing as a public instance variable in Smalltalk. The only way you can get to an instance or class variable in Smalltalk is to expose it with a method. I can see this being a great thing, instead of allowing a possible free-for-all on public variables like in Java because a programmer didn't understand the concept of encapsulation.

Blocks of Code

You can store a block of code in a variable, send the reference around and execute it later. That seems a lot nicer than function pointers in C/C++ or the "handler" design pattern often used in Java.

Small Language

The language itself is quite small, it is the extensive class library that contains most of the functionality. This makes the language itself easier to understand.

If you're curious about more differences, James Robertson recently pointed to a post from David Buck that goes through some advantages of Smalltalk over Java.

Posted at June 03, 2004 at 04:54 PM EST
Last updated June 03, 2004 at 04:54 PM EST
Comments

One small correction - you say above:

"Classes and instances still have methods, but you don't "call" them like C, C++ or Java. Instead you send the instance a message. If the instance has a method that understands the message, it's dealt with by that method. Otherwise the message is ignored."

As it happens, messages that get sent to an object that has no implementation of that method do not get ignored - an exception - MessageNotUnderstood - gets raised. That's handy, because:

1) When it's an inappropriate message, you find out (typically during testing)

2) In distributed systems, being able to handle this exception makes it trivial to implement proxies for remote objects

» Posted by: James Robertson at June 4, 2004 04:44 PM

Good to know, thanks James. :)

» Posted by: Ryan at June 4, 2004 05:24 PM

Here is a simple application to the message id... I have a whole bunch of unrelated objects that happen to have a 'Name'... it is nice that is Smalltalk, they will need not implement an IHasName interface.

» Posted by: aforward at June 9, 2004 02:46 PM

Blocks of Code?

Aka properties in .Net?

» Posted by: aforward at June 9, 2004 02:47 PM
Google
 
Search scope: Web ryanlowe.ca