So what’s so cool about (GNU) Smalltalk?

Posted: 2011/06/23 in programming
Tags: ,

Okay, I admit that just printing some text won’t make you a programmer (see my previous post). But what’s so cool about Smalltalk? The first thing I couldn’t believe of were keyword messages.

Normally in PHP you call a function / method like this:

str_replace('e', 'oa', 'test');

This will, as you might guess, replace e in test with oa, resulting in the String 'toast'. In Smalltalk messages are more human-readable (thus the name message?!). So we simply type:

st> 'test' replacingRegex: 'e' with: 'oa'

'test' is the String object of course, to whom is send the message #replacingRegex: with:. Note that both words are the signature of the message!

Let’s look at another example and create an Array and insert two elements:

st> test := Array new: 20
(nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil )
st> test at: 1 put: 'sometext'
st> test at: (test size) put: 99
st> test
('sometext' nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil 99 )

Some explanations here: at first we create an Array object with initial 20 elements and assign it to the variable test. The gst interpreter shows us immediately the freshly generated object with its 20 empty elements. Even nil is an object – to be exact it’s an instance of the class UndefinedObject.

Now let’s loop a while (to be exact, 5 times):

st> 1 to: 5 do: [ :anInteger | anInteger printNl ]

Uhm, what’s that? Let us read the statement step by step:

  • 1 is an Integer object to which we send the message #to: do:
  • 5 is the first argument of the message #to: do: – which means: loop 5 times (from 1 to 5)
  • now comes the hard part, which has no counterpart in PHP (but in JavaScript!). The code between the square brackets is a block. Think of a block as an function you can assign to variables or, as in our example, can be passed to messages. But as we are Smalltalkers now, a block is of course not a function but an object! :anInteger is a value the block receives. After the | the statements which have to be executed inside the block are defined. So our block receives the result of the #to: do: message – an Integer object. This Integer object is passed to the variable :anInteger which is printed afterwards.

But why the number 1 is printed at last again? Because Smalltalk returns the last created object in the current level of execution. Remember that if we would just create an Integer instance, we would get the object, too:

st> 1

Edit: see first comment which corrects this :-)

  1. Martin Kobetic says:

    Just a minor correction of the explanation at the end, as to why the final 1 is printed. It’s customary that in these sort of textual read-evaluate-print environments (of any language) the print part prints the result of the evaluated expression. In your example, that would be the result of the to:do: message, which in case of to:do: is simply the receiver. In your example the receiver is number 1. So, first you get the numbers 1 to 5 printed by the iterations of the block argument and then finally the result of the to:do: message which is the receiver: 1.

    • dubst3pp4 says:

      Thank you for the explanation! This was one of the points I really stumbled about at the beginning (although you can run PHP in an interactive shell, too, I never tried it) :-)

  2. Just note about your javascript statement “But as we are Smalltalkers now, a block is of course not a function but an object!”. There is no real difference between smalltalk and javascript in this regard. In Smalltalk [ ] gives you a BlockClosure (in a modern vm) and in javascript function() {} gives you a Function object. In javascript a function inherits from Object as the smalltalk BlockClosure does.

    smalltalk: [] class superclass -> Object
    javascript: (function() {}).prototype -> Object

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s