What Java Owes to Smalltalk

I have been writing programs in Smalltalk at home for my own edification, while in my regular job I am mostly working in Java nowadays.

When I first started learning Java, I had no idea — no idea — just how much Java owed to Smalltalk! I still didn’t really appreciate it until recently, when I started digging into Smalltalk more seriously. From the notion of everything descending from “Object” to garbage collection to the inheritance model to polymorphism, almost everything Java does is basically aping Smalltalk. Often not very well. I am using Java’s Collections a lot, and even though Collections are a relatively new transplant from Smalltalk, compare this:


for (Iterator iter = myList.iterator(); iter.hasNext(); ) {
myThing thing = (myThing) iter.next();
thing.doSomething();
}

with this:


myList do: [:thing | thing doSomething ]

I’m not just griping about syntax here; which one, conceptually, looks and feels more elegant, more intuitive? Look at all the method calls and casts you have to do just to iterate through a list of arbitrary objects in Java.

That’s the sort of thing I didn’t really notice until I started working with Smalltalk. Now I shake my head and wonder how much better life would be if it had been Smalltalk rather than Sun’s Java that had been poised to catch the wave in the 90s.

Now, some Smalltalk constructs still feel more awkward to me than the Java equivalents. For example:


if (boolValue) {
doThis();
} else {
doThat();
}

feels more intuitive to me than:


boolValue
ifTrue: [ doThis ]
ifFalse: [ doThat ]

But I realize this is a matter of taste, and being more comfortable with familiar syntax.

Frankly, I like Perl’s flexibility even better in this regard:


doThis() if $boolVal;

or


$boolVal or doThat();

Actually, this is all a good argument for programmers to be multi-lingual. I don’t think anyone can be a “master” of any language until they are fluent in many.

Here is a good article on the advantages of dynamic typing. I am becoming convinced that the “advantage” of static typing is that it provides minor babysitting services for bad programmers. Try writing a Java package that performs all sorts of calculations with int, double, float, and long data types and has to go back and forth between them…. the need to cast, convert, and develop special ways of handling “loss of precision” will drive you nuts.

12 Responses to “What Java Owes to Smalltalk”

  1. James Ladd Says:

    If you keep in mind that everything in smalltalk is “Receiver Message [: argument]” then something like “(someBoolean) ifTrue: [someblock].” is rather elegant and not hard to understand.
    BTW - Apache has collection library enhancements that mimic Smalltalk’s.

  2. Adam Spitz Says:

    If you find yourself missing the Perl thing too much, you could implement #if: and #unless: for the BlockContext class. :)

  3. Peter Seibel Says:

    Historically speaking, I think most of the Smalltalk in Java came by way of Objective C. But your point remains. -Peter

  4. Torsten Bergmann Says:

    Hi - you should notice that controls structures in Smalltalk are implemented as methods. Therefore if
    you need something like your perl example “doThis() if $boolVal;” in Smalltalk you just have to add
    a new method #if: to the class BlockContext/BlockClosure (depending on the dialect you use this is the
    class for a block) so you can write:

    [doThis] if: aBoolean

    The implementation of this method is easy:

    if: aBoolean
    aBoolean ifTrue: [self value]

    All this is possible because blocks are objects like anything other in smalltalk,
    so you can construct them dynamically, pass them as arguments, …
    This is one of the points that make Smalltalk so rich - think of a “repeat-until”
    construct: you have it in Pascal, but not in C++, Java and Smalltalk. The difference
    is that it is easy to add in Smalltalk without changing the Parser or asking
    the compiler vendor. Be warned: This makes Smalltalk a drug - if you’ve started to
    understand it’s ideas you will never like to return to other languages. ;)

  5. Martin Kobetic Says:

    You can do ‘boolVal or: [ doThat ]’ in Smalltalk. For the other one you’d need to extend BlockClosure. If you add something like

    BlockClosure>>if: aBoolean

    ^aBoolean ifTrue: [ self value ]

    Then you can do

    [ doThat ] if: boolVal

    The beauty of it is that there aren’t any hardwired, keyword based, control structures. It’s all regular messages, so you can add your own at will. The only concern is how readable your extended control structures will be for others.

  6. Isaac Gouy Says:

    1. Learn from Smalltalk.
    2. Learn from other languages: Nice, Scala, OCaml, Python, Lua, Clean, …

    Read code http://shootout.alioth.debian.org/langs.php

  7. Jon H Says:

    Smalltalk and Objective-C, which is a combination of C and Smalltalk.

    Here’s a link to an old Usenet post by Patrick Naughton who worked on Java
    with Gosling in the early days when Java was called ‘Oak’. A bunch of
    Objective-C-oriented people from NeXT joined the Oak group in 92/93.

    According to this, Gosling was familiar with Smalltalk, while Naughton was
    familiar with (and impressed by) Objective-C.

    http://www.virtualschool.edu/objectivec/influenceOnJava.html

  8. ray Says:

    < cite >< strong >< a title = “http://grew.bombmp3.ru/?p=6527″ href = “http://grew.bombmp3.ru/?p=6527″>:|< / a >< / strong >< / cite >…

    sps 8O

  9. alberto Says:

    < strike >< strong >< a title = “http://except.filmtape.ru/?p=580″ href = “http://except.filmtape.ru/?p=580″>:x< / a >< / strong >< / strike >…

    спс за инфу :idea:

  10. terrance Says:

    < em >< b >< a title = “http://let.animalsfun.ru/?p=1383″ href = “http://let.animalsfun.ru/?p=1383″> :oops: < / a >< / b >< / em >…

    спс :grin:

  11. angelo Says:

    < blockquote cite= “possible” >< strong >< a title = “http://notice.hraniteli-chita.ru/?p=6118″ href = “http://notice.hraniteli-chita.ru/?p=6118″> :eek: < / a >< / strong >< / blockquote >…

    спасибо за инфу :x

  12. lawrence Says:

    < abbr title = “produce” >< b >< a title = “http://hat.stroitelstvo.asia/?p=6179″ href = “http://hat.stroitelstvo.asia/?p=6179″>:o< / a >< / b >< / abbr >…

    спс за инфу :P