Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

After Java 8 is there any benefit to using Scala? It seems Java has caught up and has the benefit of being lighter.


For people interested in functional programming, I think Scala still has the following things going for it,

- 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.


Case classes, default arguments, type inference, Spark, an ecosystem that's already moved off null and off exceptions, unified type hierarchy with value types, much nicer async API, higher-kinded types. Just generally replacing all the magic annotations and XML with plain code. I mean if most of the good stuff in Java 8 is taken from Scala then why not get a head start on the kind of stuff that will be in Java 9/10/11?


Yes, case classes!

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.


well if you don't need hashCode, copy, toString etc you can use:

    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.


For the sake of comparison the Scala equivalent of those 8 lines is:

    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).


I've found that Project Lombok solves this problem for me. Simply add a @Data or @Value annotation and it will generate getters/setters/hashCode/toString etc. https://projectlombok.org/features/Value.html


I started with Lombok but found that you're effectivly writing a different language from Java anyway - all your tools (IDEs, code coverage, binary compatibility checking, build infrastructure... ) need to support it. So it's just as much effort as Scala, and gives you a lot less in return.


Scala still has several differences to Java including a stronger type system, traits, pattern matching, and implicits (love them or hate them.) Java is definitely closing the gap on some of the functional aspects though.

Edit: Where did the basic docs on type bounds in Scala go?


Higher kinded types is a feature that sets the languages apart and is what allows for some very powerful polymorphic code to be written in Scala.


Definitely agree with that. Was looking for a page in the overview section, to drop a link, but that entire topic seems to be gone?


Java will never encourage or make functional programming comfortable. The current attempts is like putting lipstick on a pig, with an attitude of anti-intelectualism, starting from Optional breaking the functor laws.

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.


I'm unconvinced that lacking scalaz is a weakness.


  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.


Java will never catch up to Scala, having to maintain backwards compatability for decades old code bases alone is anchor enough to keep Java in place relative to Scala.

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.

[1] http://dotty.epfl.ch/#so-features


Scala still has a very powerful type system. Generics in Java are almost entirely syntactical sugar (due to type erasure).

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.


> Generics in Java are almost entirely syntactical sugar (due to type erasure).

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.


Java generics are being upgraded. Declaration site variance is coming:

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).


Declaration site variance is good, except that for Java I have doubts because it's being added in addition to what it currently has.

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.


Foo<any T> reifies T. Foo<T> does not.


Java still has a ways to go before it catches up with the features of scala. Here are a couple:

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)


Java is still missing a huge amount of what makes Scala great. Most importantly case classes, pattern matching, more expressive types and traits (multiple inheritance).

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.


On the other hand one could say, since java is slowly incorporating all of scala, why not just use scala now. No waiting for jankier implementations of the same concepts.


The Java streams syntax is still painful.


I got used to the syntax pretty fast, to the point that I actually kind of like it. My complaint about streams is how slow they can be. If you're doing collection transformations often, the time spent turning your objects into streams can kill your performance. I'm worried it's going to reinforce the old myth that functional programming is implicitly slow.


It will be a lot better with value types in Java 10 (hopefully).


That's one reason (of many) that we use Eclipse collections, formerly known as GS Collections. :) It also closes the gap on weaknesses in JCL like immutability vs mutability, and a nice improvement to the interfaces.

I know even use it in personal projects in place of JCL wherever I can.


Not a scala user, but studied it.

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.


Try Kotlin - much easier, works with java and js and still have many functional features.


And the compile time is comparable to Java's. One of the most import thing imo.


Someone measured it and and it is fact it's substantially slower than Scala: https://twitter.com/kmizu/status/817570546179194881

Kotlin marketing != reality, it seems.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: