23

With closures being added to Java, what is Scala's advantage over Java as a language choice?

Can someone elaborate on any advantages?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
chen
  • 4,302
  • 6
  • 41
  • 70
  • 12
    Welcome to Stack Overflow. Please remember to "accept" the correct answer to your question once you've found it. This helps other contributors know that your question has been resolved and rewards the one who helped you. – Gunslinger47 Oct 02 '10 at 07:28
  • 7
    AFAIK, Oracle has already defined that Java 7 won't have closures. That has been postponed for Java 8, some years away still. – Daniel C. Sobral Oct 02 '10 at 18:30

3 Answers3

70

Apart from closures (which Java doesn't appear all that close to having), here's a list of features in Scala that are missing from Java. I'll omit libraries here and concentrate on the features of the language itself. This is not comprehensive by any means, but I think it contains the big ticket items.

  • Implicit parameters / conversions
  • Pattern matching, case classes
  • Type inferencing (some)
  • Higher-kinded types (abstraction over type constructors)
  • Monadic for comprehensions
  • Variance annotations
  • Interfaces with behavior (traits)
  • Default and named arguments
  • Unified methods and operators (methods can be used as infix operators, operators can be overloaded because they're just methods)
  • Unified type hierarchy; no primitive types
  • Properties rather than getters and setters
  • Abstract values
  • First-class immutable references (vals are as easy to declare as vars)
  • By-name (lazy) terms (maybe Java closures would make this reasonably easy to express)
  • Some tail-call recursion optimization
  • Abstract types
  • Type aliasing
  • Self types
  • Path-dependent types
  • Structural types
  • Type ascription, as distinguished from type casting
  • Renaming imports
  • First-class modules (objects)
  • First-class packages
  • Reified generics (manifests)
  • Delimited continuations

Some cool secondary constructs that these building blocks enable:

  • Type classes (via implicit parameters and higher-kinded types)
  • The "Pimp My Library" pattern (via implicit conversions)
  • Internal DSLs (via operator overloading and infix methods)
  • Parser combinators (enabled by higher-order functions and made pretty by infix methods)
  • Generators, coroutines, custom control structures (via delimited continuations)
  • Type-level programming (via higher-kinded and abstract types)
  • Obsolescence of dependency injection frameworks (via the Cake Pattern)

Lastly, I'll mention that Scala has a REPL (read-evaluate-print-loop)--not really a feature of the language itself, but it's very nice to have!

Community
  • 1
  • 1
Tom Crockett
  • 30,818
  • 8
  • 72
  • 90
  • 2
    Great list, and because I can't see something like this without trying to add to it, here are some additions to consider: abstract values, abstract types, duck-typing, first class modules (objects), obsolesence of dependency injection frameworks (via cake pattern) – Dave Griffith Oct 02 '10 at 12:22
  • 1
    Good additions, I knew I was bound to forget some important ones! – Tom Crockett Oct 02 '10 at 19:18
  • 1
    Another one : the ability to precisely choose the visibility of classes, leveraging the hierarchical nature of packages. I miss that a lot in Java. – barjak Oct 02 '10 at 20:21
  • 1
    It may be useful to explain that "operator overloading" and "methods as infix operators" come from the same design choice : the fact that operators and methods are unified. – barjak Oct 02 '10 at 20:29
  • Thanks @barjak, I incorporated your suggestions. – Tom Crockett Oct 02 '10 at 20:49
  • Interfaces with behavior (traits): In Java 8 you are able to put default implementation of method in interface. This is not fair to be on the list. – Bence Olah Feb 26 '14 at 17:30
8

Even if Java 7 [8,..] will have first-class functions one day, it still lacks for implicits, type inference, powerful Collections library, pattern matching, traits and lots of other things boosting productivity. Not talking about various Actor libraries, rich capabilities of building DSLs,...

Vasil Remeniuk
  • 20,519
  • 6
  • 71
  • 81
  • 2
    I'd disagree on the Collections library part, Scala's Collections are created with Scala in mind, Java's with Java so naturally they're different in a lot of ways but they're still both excellent, patern matching and traits however are something that're really missing from Java. – Esko Oct 02 '10 at 07:51
  • 7
    Java's collections library was implemented without any thought towards immutable data structures. This is a mistake in any language. – Tom Crockett Oct 02 '10 at 09:19
  • 1
    @pelotom: Java doesn't have inherently immutable data structures at all so I don't think truly immutable collections in Java won't show up for a long while. I'd venture a guess though that what you're referring to isn't a *de facto* thing for a language, it's more of a feature used by certain kind of programmers which of course isn't a bad nor ignorable thing. – Esko Oct 02 '10 at 21:19
  • @Esko: I'm not sure what you mean. It's perfectly possible to define "inherently immutable data structures" in Java, the collections library just neglected to. – Tom Crockett Oct 02 '10 at 22:01
  • @pelotom: `final` in Java isn't same as const which means you can break it with reflection, you can't declare programmatically that your class is (deeply) immutable, Java itself doesn't support easy constructs for fast copying of objects... Nope, immutability isn't a feature Java does natively, you can get very close with clever programming (*see `String` class as en example*) but it's still not entirely immutable. – Esko Oct 03 '10 at 07:44
7

The latest I've heard is that closures won't make it into Java 7.

http://www.baptiste-wicht.com/2010/09/jdk-7-features-updated-plan-b-is-apparently-here/

http://openjdk.java.net/projects/jdk7/features/

The second link clearly lists project Lambda (closures) as deferred to JDK 8.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216