tags:

views:

429

answers:

2

I am exploring Scala language. One claim I often hear is that Scala has a stronger type system than Java. By this I think what people mean is that:

  • scalac rejects certain buggy programs which javac will compile happily only to cause a runtime error
  • certain invariants can be encoded in a Scala program such that the compiler won't let the programmer write code that violates the condition

Am I right in thinking so? If so, please point to articles/blogs/papers which illustrate such examples.

+18  A: 

The main advantage of the Scala Type system is not so much being stronger but rather being far richer (see "The Scala Type System").
(Java can define some of them, and implement others, but Scala has them built-in).
See also The Myth Makers 1: Scala's "Type Types", commenting Steve Yegge's blog post, where he "disses" Scala as "Frankenstein's Monster" because "there are type types, and type type types".

VonC
@VonC Thanks for the detailed write up. I will read the linked articles to get a better understanding.
binil
Note to self: see also my old answer http://stackoverflow.com/questions/2682673/what-compromises-scala-made-to-run-on-jvm/2682962#2682962
VonC
Note to self: with implicit and the new 2.8 context bound, pattern like "Type Class" are easily defined and used: http://dcsobral.blogspot.com/2010/06/implicit-tricks-type-class-pattern.html
VonC
Note to self: beware of structural typing used with the "pimping my library" pattern: there can be severe performance issues. See http://www.scala-notes.org/2010/06/avoid-structural-types-when-pimping-libraries/
VonC
+5  A: 

The main safety problem with Java relates to variance. Basically, a programmer can use incorrect variance declarations that may result in exceptions being thrown at run-time in Java, while Scala will not allow it.

In fact, the very fact that Java's Array is co-variant is already a problem, since it allows incorrect code to be generated. For instance, as exemplified by sepp2k:

String[] strings = {"foo"};
Object[] objects = strings;
objects[0] = new Object();

Then, of course, there are raw types in Java, which allows all sort of things.

Also, though Scala has it as well, there's casting. Java API is rich in type casts, and there's no idiom like Scala's case x: X => // x is now safely cast. Sure, one case use instanceof to accomplish that, but there's no incentive to do it. In fact, Scala's asInstanceOf is intentionally verbose.

These are the things that make Scala's type system stronger. It is also much richer, as VonC shows.

Daniel
Finally, an answer which is more "on topic" than mine "more general" list ;) +1
VonC
@Daniel, you answered the original question accurately, but VonC answered a slightly different question elaborately. I am voting his answer as the correct one, but your answer is much appreciated too. Hope you don't mind missing out on the reputation points.
binil
@binil Not at all. VonC's answer is great, even if he really doesn't need the extra reputation... ;-)
Daniel