Thursday, June 14, 2007

Some Thoughts on Smalltalk

As part of my OO class, I'm learning Smalltalk.  More specifically, I'm learning Squeak which is an open implementation of Smalltalk.  What follows are some of my observations about the language.  I'm assuming that most readers are unfamiliar with it as Smalltalk is not one of the popular languages any more.

  • Smalltalk is a very small core language.  The syntax describes classes, objects, variables, blocks (closures), and messages.  That's about it.  All method calling is via message passing.
  • Everything else is in the library.  Even most control flow operations are part of the library, not language primitives.
  • The library is all open.  Not only the library, but the entire environment.  Everything has its source code open.  Not just source code that is available for download and compiling.  It's all in the environment.  When you debug an error being thrown, you can debug through the error generation and handling code.  Even the UI from which you called the offending code is at the top of the stack.  This has the result that you can modify *everything* about the system should you want to.
  • Because everything is open, the library feels chaotic.  Often the same functionality is implemented in different ways in different parts of the system.
  • Convention rules over language features.  Whereas in most languages there is a syntax for pure virtual functions or interfaces, in Smalltalk, there isn't.  When you want to declare a pure virtual function, you just have it call subclassResponsibility (or subclassResponsibilityMarker or fred).  If you want to instantiate an abstract class, you can.  There are no constructors.  There are no truly private methods.  Both are merely conventions.
  • There are no source files.  Everything is done in the programming environment.  There is a way to export .st files but a) they apparently aren't standard and b) they are only partially human readable.  If you want to add a method, you click here.  If you want to create a class.  Click there.  As someone that uses Vim to code, this is very strange indeed.  This also has the effect of rendering standard tools like grep and regex useless.
  • Everything is dynamic.  There are no types.  There are no (or few) errors found by the compiler.  If you want to pass an array to something that takes a string, everything will work until you try to use a string method, when it pukes.  If there is a syntax bug in your code, you won't find it until you actually execute the method.  No wonder these guys pioneered unit testing.
  • Precedence rules are strange.  There are three kinds of messages and they are each executed in a specific order.  Within each class, the ordering is left to right.  Standard math conventions are ignored.  8 + 3 * 6 is 66, not 26.
  • Polymorphism comes from having messages with the same name, not from an interface as modern languages define them.  There is no inheritance involved.  It's almost like calling methods inside template code or macros in C++.
  • All data is protected.  The only way to access class/instance variables is through methods.

 

Smalltalk is a very interesting language, but it is old.  Newer languages have refined the concepts and implemented them better.  I don't think Smalltalk makes a great production language anymore.  However, there is a lot useful here.  Learning it can be beneficial.

7 comments:

  1. Hi , nice blog.
    Only one correction, there is no question of operator based precedence since Smalltalk do not have operators. Smalltalk evaluates messages left to right. Precedence is based on messages as everything.
    Unary messages are sent first. Thus having the highest precedence
    Then binary messages.
    Keyword messages are sent last.
    You will learn a lot from Smalltalk, and you are going to love it :)

    ReplyDelete
  2. "No wonder these guys pioneered unit testing."
    I like that quote.  
    I wonder how many of these XP-->Agile development processes are really a result of the programming language (e.g. dynamic OO).

    ReplyDelete
  3. @Hiren - This is what I was trying to convey but you've said it more accurately.  Coming from a language with operators and precedence (every other language I can think of), it's strange not to have it in the language.
    @Dion - Probably a lot.  Most of the early XP thinkers came from the world of Smalltalk.  In fact, there was a Smalltalk version of JUnit before it was called JUnit.

    ReplyDelete
  4. Hi Steve :)
    Smalltalk version of JUnit is called SUnit, i use it daily.

    ReplyDelete
  5. Ralph Johnson says the first smalltalk version was called "Testing Framework" and written by Kent Beck.  What is now SUnit traces its lineage through JUnit which itself was inspired by Testing Framework.

    ReplyDelete
  6. For your "there are no source files" comment, I would suggest you to try GNU Smalltalk.  I'm releasing 2.95b today.
    Paolo

    ReplyDelete
  7. Hi Steve,
    "Newer languages have refined the concepts and implemented them better."
    Which languages do you mean?
    Which concepts do they refine and implement better?
    In which respect?
    "I don't think Smalltalk makes a great production language anymore."
    Smalltalk is used every day all over the world in business critical applications. It is not only in production but often makes the competetive advantage! This applies to Squeak in the same way as to commercial dialects. You should also take a look at:
    Cincom Smalltalk
    VA Smalltalk
    GemStone/S
    Dolphin Smalltalk
    and a lot more...
    Cheers
    Helge

    ReplyDelete