Dec 9, 2009

Groovy, Scala and Clojure – A Comparison

We recently started off on a couple of projects that target the JVM. The team had no prior Java experience but had worked on C#/.Net. We found Java tedious and ceremonial and decided to investigate the other languages that target the JVM – Groovy, Scala and Clojure. These notes summarize findings based on my interpretation of the language specs, examples and scanning the project sites. They are not based on any real programming experience on these languages.
1. Groovy
  • Language
    • Syntax very similar to Java, therefore easy for a Java programmer to pick up. Semantically seems to be influenced from Ruby
    • Scripting language – the engine can be used in the JSR 223: Scripting for the Java Platform API classes
    • Dynamically typed
    • Eagerly evaluated
  • Why not use JRuby or Jython instead to target the JVM?
    • Groovy is targeted to the Java developer – superset syntax and tighter integration
    • So if you have a background with Python or Ruby, you would use JRuby or Jython. Java libraries syntax may feel awkward while being called from a Ruby / Python
  • Frameworks
    • Grails – Rails inspired
    • Griffon – for the desktop
  • Projects - http://groovy.codehaus.org/Related+Projects
  • Books – Groovy in Action seems to be the authoritative one
  • Community - http://groovy.codehaus.org/Community+and+Support

2. Scala
  • Language
    • Statically typed, Type inferred
    • Not a scripting language like Groovy
    • OO with functional features
    • Targets both JVM and CLR (with quirks)
    • Eagerly evaluated
    • Higher order functions
    • Automatic closures
    • Mixins and Traits
    • List Comprehensions
    • No concept of static (fields, methods, classes) – singletons instead
    • Tail call optimization
    • Concurrency support – Actors (like Erlang)
  • Frameworks – Lift – Rails like
  • Books – Programming in Scala – written by Martin Odersky himself
  • Community - http://www.scala-lang.org/node/1707

3. Clojure
  • Language
    • Lisp dialect that targets the JVM and the CLR
    • Focus on functional programming and concurrency
    • Immutable objects
    • Macros
    • Multi-method dispatch instead of OO (see http://en.wikipedia.org/wiki/Multiple_dispatch to understand this)
    • Tail call optimization – via recur
    • Concurrency support – Refs (STM), Agents and Atoms
  • Books – Programming Clojure
  • Community - http://clojure.org/community

Conclusion
  • Groovy - If you are an experienced Java programmer and need to do scripting work.
  • Clojure – If you have a Lisp background and want to target the JVM. Take the high ground on concurrency – very promising.
  • Scala – If you want sophistication, and the best of OO and functional. Most impressive.
  • Cool part is that like the CLR, you can do language interop here – so choose the best language for the job
Personally, I am excited about Scala – I think it will replace Java as the mainstream language on the JVM in the long-term. Clojure is exciting because I always wanted to learn Lisp – here’s the perfect opportunity!

2 comments:

Satpal said...

Saltmarh Media is organizing Great Indian Developer Summit event in Bangalore. This Summit will be a boost for the Software Developing Industries. It covers the topics like .Net, Java and Richweb and has 1 day workshop at the end as well. Any one attneding this event?

Register @ www.developersummit.com

Shantanu Kumar said...

(Disclaimer: I am the author of Bitumen Framework [1])

Interesting post. Having dabbled in Groovy, Scala and Clojure, I tend to assume now that neither of these may replace Java. Java is a kitchen sink language - you need to omit/correct some features to make it good, for example:

1. Data mutability and bashing-in-place
2. Sharing of mutable state (this is one damn leaky abstraction)
3. Extensible concrete classes
4. Poor meta-programming (reflection)
5. Lack of autonomous actors like Erlang/BEAM

To replace Java we need a simpler language that can do more. I hope the next big language will borrow the best features from other languages/paradigms.

My assessment of the three languages is as follows:

Groovy:- Interesting language, can use it as an add-on to Java [2]
Clojure:- Well implemented Lisp on the JVM, Intellectual leap from Java [3]
Scala:- Interesting features, complex type system, debatable Java compatibility (namespace mangling)

My biggest complaint against Scala is - it is more complicated than Java and hasn't got rid of the major problems in Java.

[1] http://code.google.com/p/bitumenframework/

[2] http://charsequence.blogspot.com/2010/01/replacing-application-properties-with.html

[3] http://bitumenframework.blogspot.com/2009/09/benefits-of-using-clojure-lisp-in.html