Scala The Groovy Killer?

Share the article!

Excellent! Now that I’ve got your attention, let me explain.

Mike Spille has a couple of blog entries analyzing the pros and cons of Groovy. In his most recent one, he makes a couple suggestions on improving the clarity of Groovy’s closure syntax. Here’s what he came up with:

1) Simple form
      [1, 2, 3, 4].each () => { count += it; last = it }


2) With parameters
      c = (a, b,c) => { println("Hello ${a} ${b} ${c}") }


3) More complex in-line usage with variables
      value = [1, 2, 3].inject('counting: ') (str, item) => { str + item }


4) Trivial assignment
      salutationClosure = ()=>{ println ("Hello there ${it}" }

Coincidentally, that just happens to be almost identical to the syntax that another Java based language. The language is called Scala, and its built by the folks who formerly brought you GJ the main inspiration for adding generics to JDK 1.5. Here’s a snippet of their “lightweight” anonymous function (i.e. closure) syntax:

1) Simple form
x: Int => x + 1

2) Multiple parameters
(x: Int, y: Int) => "(" + x + ", " + y + ")

3) No parameter
() => { System.getProperty("user.dir") }

The core difference however between Scala and Groovy is that the former is statically typed. Some may argue that the makes it more cumbersome for scripting purposes, the main motivation behind groovy. Nevertheless, Scala has a whole boat load of features that make Groovy look more like a toy. For example, they have “sequence comprehensions” (reminiscent of list comprehensions1). This stuff leads to extremely compact and powerful expressions of algorithms, for example the n-queens problem:

def queens(n: int): List[List[int]] = { 
    def placeQueens(k: int): List[List[int]] = 
        if (k == 0) List(List()) else 
           for (val queens <- placeQueens(k - 1); 
                 val column <- List.range(1, n + 1); 
                 isSafe(column, queens, 1)) 
                 yield col :: queens; placeQueens(n); } 

What's more Scala has even more features that may prove extremely useful such as nested classes, currying and algerbraic type pattern matching. It also supports generics and variance annotations that may be similar in spirit to the JDK 1.5 additions. That's just the tip of the iceberg, you'll find even more features at the Scala site that I've failed to mention simply because I have yet to figure them out.

Of course, one final note, unlike Groovy which is still Beta, Scala has been 1.0 since January. I guess it would not be a significant task to remove all its static typing capabilities and some other features that may require it, to build a dynamically typed version. People who've written a compiler before know quite well that its easier to build dynamically typed languages, so such an endeavor would really mean a dumbing down of the original code.

But rather than see a battle to the death, I would rather like to see the more pragmatically minded Groovy folks get together with the more academically minded Scala folks to crank out a language that is not only more powerful but easier to use. Martin Odersky's group has been building Java based languages for years, it's vitally important that they be included in the expert group for JSR-241 or its equivalents2. While I'm at it, I should just as well invite a whole bunch of other parties.

1. In the last start-up company I was in, we used comprehensions extensively to formulate expressions that looked almost like database queries.


2. How does "Scoovy" sound? Better yet how about "Grala".


Share the article!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>