133

On the surface Groovy and Scala look pretty similar, aside from Scala being statically typed, and Groovy dynamic.

  • What are the other key differences, and advantages each have over the other?
  • How similar are they really?
  • Is there competition between the two?
    • If so, who do you think will win in the long run?
Leif
  • 1,431
  • 2
  • 10
  • 8
  • Groovy 2.0 also includes static typing: http://www.infoq.com/articles/new-groovy-20 – Dave Aug 07 '12 at 21:57

6 Answers6

237

They're both object oriented languages for the JVM that have lambdas and closures and interoperate with Java. Other than that, they're extremely different.

Groovy is a "dynamic" language in not only the sense that it is dynamically typed but that it supports dynamic meta-programming.

Scala is a "static" language in that it is statically typed and has virtually no dynamic meta-programming beyond the awkward stuff you can do in Java. Note, Scala's static type system is substantially more uniform and sophisticated than Java's.

Groovy is syntactically influenced by Java but semantically influenced more by languages like Ruby.

Scala is syntactically influenced by both Ruby and Java. It is semantically influenced more by Java, SML, Haskell, and a very obscure OO language called gBeta.

Groovy has "accidental" multiple dispatch due to the way it handles Java overloading.

Scala is single dispatch only, but has SML inspired pattern matching to deal with some of the same kinds of problems that multiple dispatch is meant to handle. However, where multiple dispatch can only dispatch on runtime type, Scala's pattern matching can dispatch on runtime types, values, or both. Pattern matching also includes syntactically pleasant variable binding. It's hard to overstress how pleasant this single feature alone makes programming in Scala.

Both Scala and Groovy support a form of multiple inheritance with mixins (though Scala calls them traits).

Scala supports both partial function application and currying at the language level, Groovy has an awkward "curry" method for doing partial function application.

Scala does direct tail recursion optimization. I don't believe Groovy does. That's important in functional programming but less important in imperative programming.

Both Scala and Groovy are eagerly evaluated by default. However, Scala supports call-by-name parameters. Groovy does not - call-by-name must be emulated with closures.

Scala has "for comprehensions", a generalization of list comprehensions found in other languages (technically they're monad comprehensions plus a bit - somewhere between Haskell's do and C#'s LINQ).

Scala has no concept of "static" fields, inner classes, methods, etc - it uses singleton objects instead. Groovy uses the static concept.

Scala does not have built in selection of arithmetic operators in quite the way that Groovy does. In Scala you can name methods very flexibly.

Groovy has the elvis operator for dealing with null. Scala programmers prefer to use Option types to using null, but it's easy to write an elvis operator in Scala if you want to.

Finally, there are lies, there are damn lies, and then there are benchmarks. The computer language benchmarks game ranks Scala as being between substantially faster than Groovy (ranging from twice to 93 times as fast) while retaining roughly the same source size. benchmarks.

I'm sure there are many, many differences that I haven't covered. But hopefully this gives you a gist.

Is there a competition between them? Yes, of course, but not as much as you might think. Groovy's real competition is JRuby and Jython.

Who's going to win? My crystal ball is as cracked as anybody else's.

James Iry
  • 19,367
  • 3
  • 64
  • 56
  • "Is there a competition between them? Yes, of course, but not as much as you might think. Groovy's real competition is JRuby." Don't forget about Jython! – Jeff Apr 02 '09 at 23:45
  • Oh, right, Jython! I'll add that to my answer. Thanks – James Iry Apr 02 '09 at 23:50
  • It's worth noting that Clojure's multiple dispatch can dispatch on values as well as types. Strictly speaking, I believe it is just as powerful as Scala's pattern matching, though Scala gets an edge if you add the patterns allowed by typeclasses (implicit values and conversions). – Daniel Spiewak Apr 03 '09 at 16:06
  • 22
    it'd be a win for both if one can get universities to start teaching these languages instead of just java =) – Chii Apr 07 '09 at 12:28
  • A month or so ago it seemed like the Groovy guys might re-write the benchmarks game programs after the release of Groovy 1.6.0 - so in vain hope Groovy was half-included in the current benchmarks game http://shootout.alioth.debian.org/u64q/benchmark.php?test=all&lang=groovy&lang2=scala&box=1 – igouy Apr 09 '09 at 17:13
  • 13
    Isn't immutability a key characteristic of Scala ? What about concurrency and actors ? Tell us some more... – Leonel Apr 09 '09 at 19:34
  • 4
    If there is any competition it would be with Clojure, but Clojure isn't interested in competition. – Rayne Apr 11 '09 at 21:17
  • Can you elaborate on the statement "Pattern matching also includes syntactically pleasant variable binding. It's hard to overstress how pleasant this single feature alone makes programming in Scala."? I can ask a separate question if you want... – oxbow_lakes Apr 16 '09 at 11:24
  • 1
    You said that Groovy emulates call-by-name with closures, but (at least superficially) it seems to me that it uses maps to mimic named params – Dónal Jul 17 '09 at 21:33
  • >> Finally, there are lies, there are damn lies, and then there are benchmarks. << "After all, facts are facts, and although we may quote one to another with a chuckle the words of the Wise Statesman, 'Lies--damned lies--and statistics,' still there are some easy figures the simplest must understand, and the astutest cannot wriggle out of." Leonard Henry Courtney, 1895 – igouy Apr 03 '10 at 03:36
  • 2
    Given that Scala uses the same static typed method dispatch as java (which hotspot can easily inline) and groovy does dynamic method dispatches its going to be really tough for Groovy to ever get close to Scala's performance. Particularly with @specialised to optimise java's autoboxing, Scala can be faster than java at times. However the use case for Groovy is similar to using Ruby / Python - its for an easy to use dynamically typed scripting language where performance is typically not that much of an issue. e.g. many Groovy frameworks contain a ton of Java code for performance (like Grails) – James Strachan Jul 21 '10 at 11:49
  • 4
    Since this answer has so many votes, there is one part I would like to correct. It is right that inheritance based multimethods in Groovy started out as accident, but on a Groovy Developers Conference, that included James and that was long before Groovy 1.0 we decided to keep that. It would not have been difficult to change this. Also to add to what James wrote... invokedynamic took down that barrier he is talking about – blackdrag Feb 19 '13 at 17:37
  • @Dónal Scala's "call-by-name parameters" are a completely different thing than named ones. According to the specs, a call-by-name parameter "is not evaluated at the point of function application, but instead is evaluated at each use within the function." To do the same thing in Groovy you would in fact use closures. It's only a syntactic difference: wrapping braces around the actual arguments, at the function call site, and appending parentheses to each use of the formal arguments, in the function body. For example: `def f(x,y) { x() + y() }; println f({1}, {2})` – Tobia Mar 10 '15 at 22:46
  • "Scala does direct tail recursion optimization" - I thought Scala can't do tail recursion optimization due to JVM limitations. Is that false? – Honinbo Shusaku Jan 21 '17 at 00:40
13

scala is meant to be an oo/functional hybrid language and is very well planned and designed. groovy is more like a set of enhancements that many people would love to use in java. i took a closer look at both, so i can tell :)

neither of them is better or worse than the other. groovy is very good at meta-programming, scala is very good at everything that does not need meta-programming, so...i tend to use both.

hamsterofdeath
  • 131
  • 1
  • 2
11

Scala has Actors, which make concurrency much easier to implement. And Traits which give true, typesafe multiple inheritance.

jasonnerothin
  • 1,556
  • 10
  • 15
7

You've hit the nail on the head with the static and dynamic typing. Both are part of the new generation of dynamic languages, with closures, lambda expressions, and so on. There are a handful of syntactic differences between the two as well, but functionally, I don't see a huge difference between Groovy and Scala.

Scala implements Lists a bit differently; in Groovy, pretty much everything is an instance of java.util.List, whereas Scala uses both Lists and primitive arrays. Groovy has (I think) better string interpolation.

Scala is faster, it seems, but the Groovy folks are really pushing performance for the 2.0 release. 1.6 gave a huge leap in speed over the 1.5 series.

I don't think that either language will really 'win', as they target two different classes of problems. Scala is a high-performance language that is very Java-like without having quite the same level of boilerplate as Java. Groovy is for rapid prototyping and development, where speed is less important than the time it takes for programmers to implement the code.

Don Werve
  • 5,100
  • 2
  • 26
  • 32
  • 3
    "Groovy is for rapid prototyping and development" - this suggests that Groovy is not suitable for production use which I'd disagree with. There are quite a lot of Grails sites in production use, for example – Dónal Jul 17 '09 at 21:35
  • 10
    Scala is not a dynamic language. – John Oxley Oct 17 '11 at 11:05
  • 2
    "Groovy has (I think) better string interpolation." -- with Scala-2.10.0 this is no longer true (Scala got cool string interpolation). – VasiliNovikov Aug 01 '13 at 15:12
5

Scala has a much steeper learning curve than Groovy. Scala has much more support for functional programming with its pattern matching and tail based recursion, meaning more tools for pure FP.

More Than Five
  • 9,959
  • 21
  • 77
  • 127
0

Scala also has dynamica compilation and I have done it using twitter eval lib (https://github.com/twitter/util ). I kept scala code in a flat file(without any extension) and using eval created scala class at run time. I would say scala is meta programming and has feature of dynamic complication