- Built in types (immutable persistent collections, Try, etc). You can get these in Java with http://www.javaslang.io/ but it's a relatively new library.
- A concise syntax for creating value types without using something like Lombok.
- Mature libraries (ScalaZ, Monocle, shapeless, etc) for people interested in more sophisticated functional patterns.
I currently use Java for work and I cry a little inside every time I see big hashCode() methods, toString() methods and a chain of getXXX()/setXXX() methods when I know that a simple case class statement would have been all that was needed.
final class Data() {
final int id;
final String name;
public Data(int id, String name) {
this.id = id;
this.name = name;
}
}
No need for getter/setter.
Still not as good as the scala one tought, but probably a little bit faster since it does not use accessor methods.P.S.: I'm a scala user.
case class Data(id: Int, name: String)
and comes with all the other things merb mentioned. And it's unlikely to be any slower in practice since the JIT can inline the access (not that method calls are ever likely to be a noticeable overhead anyway).Edit: Where did the basic docs on type bounds in Scala go?
Scala is one of the very few languages that makes functional programming comfortable and is among the handful able to do higher kinded polymorphism and to encode type classes. You can probably count with one hand the number of languages in which you can express Haskell's powerful FP abstractions and Scala is one of them.
You cannot express libraries such as those in Typelevel (Cats, Shapeless, etc) or Scalaz in languages like Java, C# or F# for that matter. The only other comparable languages in expressivity, maturity and potential are OCaml and Haskell, with OCaml being very similar in spirit, but less popular (and F# is no OCaml ;)).
Of course, you will never feel this unless you actually start using the language, getting past the initial hello world and Javaisms. This problem was coined by Paul Graham as the "blub paradox": you can only notice inferior languages, abstractions and paradigms to what you currently know, but you can't easily notice superior ones, unless you make an effort to learn more. The great thing about Scala is that it allows a gradual migration and although this expressivity can be seen as a weakness, it's also why Scala is probably the most popular FP language.
This problem was coined by Paul Graham as the "blub paradox":
you can only notice inferior languages, abstractions and
paradigms to what you currently know, but you can't easily
notice superior ones, unless you make an effort to learn more.And Scala itself is evolving, the new compiler, Dotty, brings a host of new features[1] including union types, implicit functions, trait parameters, etc., all while improving compilation speeds and streamlining compiler internals. Add Scala Meta (overhauls old macro system), Scala Native joining Scala.js as Scala alt-JVM targets, and you have Java 28 ;)
Saying that, Scala will likely remain a niche language, Java is king, slow and steady wins the race in the enterprise.
E.g. if you are coming from modern C++ use and you are comfortable with templates, you might be disappointed with giving up all that power in Java.
Well, from that point of view, static typing in general is just syntactic sugar. In fact type erasure is one of the best things going for Java, because they haven't screwed the runtime for other languages (e.g. Scala, JRuby, Clojure). Ironically it is the JVM that turned out to be the multi-language VM, with the CLR having only languages that have basically C#'s type system.
No, the problem with Java generics is that covariance / contravariance rules are "call site", specified with wildcards, which are awkward and hard to reason about, versus "declaration site" in Scala and C#.
Scala also has higher kinded types, one of the few languages actually. In combination with making it possible to encode type-classes, by means of plain traits along with implicits, you can express Haskell's powerful abstractions in Scala. You don't see libraries like Cats or Scalaz in other languages like Java or C# or F# for that matter, because they can't be expressed in languages without higher kinded types.
http://openjdk.java.net/jeps/300
And they're also adding type specialisation and reified generics as part of the value types work, somewhat similar to what's available in C++ (but a bit less crazy).
Specialization for value types != reification and from what I understood from their proposal they are not introducing reification, for one because they still have backwards compatibility concerns, but also because they don't want to screw other languages. I hope those plans haven't changed.
I'm sure others have said it, but type aliases can't be done in java. For instance this is useful for seeing 'PersonId' instead of 'String'. More semantically meaningful names. You can emulate it in java, but you incur a runtime cost and have to write a wrapper class.
Another interesting feature is value types. There is work in java to support this, but it's java 10 at the earliest (see https://en.wikipedia.org/wiki/Project_Valhalla_(Java_languag...). Since scala runs on the JVM which doesn't support value types there are cases where it has to allocate an object. (see http://docs.scala-lang.org/overviews/core/value-classes.html)
It caught up a lot, but it's still a jump like IE7 was from IE6 compared to the Firefox of Scala. A hell of a lot better, but still lacking.
I know even use it in personal projects in place of JCL wherever I can.
Immediately off the type of my head: case classes, pattern matching, and maybe scala.js.
Pretty sure scala still has the upper hand in total loc vs corresponding java.
That said I enjoy Java enough so its no biggie I can't find scala positions worth looking into.
Kotlin marketing != reality, it seems.