Scala expert checklist — Part 2

Dissecting Scala 2.12: 6 developers weigh in on the good, the bad, the plans

JAXenter Editorial Team
Boxing gloves image via Shutterstock

Scala 2.12 is finally here. We take a look at its main features and see what our six Scala developers have to say about it. Plus, we invited them to name something that they still miss in Scala and would like to be implemented in the next releases.

Scala 2.12: Overview

Scala 2.12 is finally here. From now on, 2.12.x releases will be fully binary compatible. However, Scala 2.11 and 2.12 are not  binary compatible, thus allowing the team to continue to improve the Scala compiler and standard library.

The Scala 2.12 compiler has been improved to make use of the new VM features available in Java 8. Therefore, a trait compiles directly to an interface with default methods —thus improving binary compatibility and Java interoperability. The Scala and Java 8 interop has also been overhauled for functional code as methods which take functions can easily be called in each direction using lambda syntax. 

Traits (#5003) and functions are compiled to their Java 8 equivalents —the compiler does not generate trait implementation classes (T$class.class) and anonymous function classes (C$$anonfun$1.class) and Single Abstract Method types and Scala’s builtin function types are now treated uniformly from type checking to the back end (#4971). invokedynamic is used for a more natural encoding of other language features (#4896), they have standardized on the GenBCode back end (#4814, #4838) and the flat classpath implementation is now the default (#5057).

The experience of using libraries written for Java 8 in Scala has also been enhanced — the Scala 2.12 type checker accepts a function literal as a valid expression for any Single Abstract Method (SAM) type, in addition to the FunctionN types from standard library. However, one should keep in mind that only lambda expressions are converted to SAM type instances, not arbitrary expressions of FunctionN type.

scala> val f = () => println("Faster!")
scala> val fasterRunnable: Runnable = f
<console>:12: error: type mismatch;
 found   : () => Unit
 required: Runnable

With the use of default methods, Scala’s built-in FunctionN traits are compiled to SAM interfaces. One can create Scala functions from Java using Java’s lambda syntax.

public class A {
  scala.Function1<String, String> f = s -> s.trim();

The new release emits bytecode for functions in the same way as Java 8, whether they target a FunctionN class from the standard library or a user-defined Single Abstract Method (SAM) type. For every lambda the compiler generates a method containing the lambda body, and emits an invokedynamic which will spin up a lightweight class for this closure using the JDK’s LambdaMetaFactory.

Check out all the changes to Scala 2.12.


Now let’s see what our six Scala developers think of Scala 2.12:

Scala 2.12 has been released. What do you find most interesting in this release?

Heiko Seeberger: Scala 2.12 actually has one goal: the integration of java 8. As users, we can now select the new Java streams with Scala by committing Scala lambdas. This also works vice versa. Under the hood, the use of new JVMm features makes the compiler generate less bytecode.

Markus Hauck: Even though it seems from the outside that nothing important happened, there were quite a few changes made „under the hood.“ The introduction of new features in Java 8 made it a lot easier for Scala to translate much of its own features to Java. Furthermore, there are a lot of small changes in the standard library, e.g. „either“ finally is right-biased and also futures got some useful new methods. And also a long awaited fix for the infamous SI-2712 is finally in the release which is particularily pleasing.

Daniel Westheide: The fact that Scala 2.12 requires Java 8 and thus improves the interoperability between both of them is in my opinion especially exciting. This means that on the one hand, some Scala constructs will make use of the Java 8 bytecode extensions, on the other hand, Scala functions can now be called easily directly out of Java 8.

Ivan Kusalic: I’m most interested in the style checker — Scala syntax is very flexible which is actually great but as a consequence, it requires extra effort to have consistency in a bigger codebase. In my team, we currently take care of that in code reviews but it takes a while for new team members to be brought up to speed.

Daniela Sfregola: There are lots of interesting new features in this release. The most exciting one for me is the introduction of parameterized traits: now trait can have parameters, making them even more flexible and easy to use. We can now get rid of abstract classes and just use trait with parameters instead :)

Julien Tournay: The better support for Java 8 bytecode is really nice. It should improve runtime performances, bytecode size and compilation speed significantly. The one thing I’m particularly excited about is to see SI-2712  finally closed. This bug has been haunting Scala developers for years, especially library authors. This fix may have also started something in the community. Hopefully, more people will submit bugfixes. I should probably contribute more myself :)

ReactiveTo read more about reactive programming, download the latest issue of JAX Magazine:

Reactive programming means different things to different people and we are not trying to reinvent the wheel or define this concept. Instead, we are allowing our authors to prove how Scala, Lagom, Spark, Akka and Play coexist and work together to create a reactive universe.

If the definition “stream of events” does not satisfy your thirst for knowledge, get ready to find out what reactive programming means to our experts in Scala, Lagom, Spark, Akka and Play. Plus, we talked to Scala creator Martin Odersky about the impending Scala 2.12, the current state of this programming language and the technical innovations that await us.

Thirsty for more? Open the magazine and see what we have prepared for you.


Please name something that you still miss in Scala and would like to be implemented in the next releases

Heiko Seeberger: Scala has accumulated quite a few years under its belt but there has nonetheless been a little ballast. Martin Odersky, the inventor of Scala, is currently working on the next scala version, Scala 3. This version will get rid of some of this ballast.

Markus Hauck: Some features that have come into the language in the context of the research work do not match 100 percent. This does not lead to problems in practice, but there are still here and there marginal conditions, where you have to scratch your head. Luckily work is on the way and the Dotty platform has already some very interesting innovations.

Daniel Westheide: All in all I am very satisfied. It would be good if the Standard library would get a canonical implementation of HList as well as a stable Macro API. I am quite curious about what will happen to Dotty that was built with the intention to simplify the Scala basics and smooth some of the stumbling blocks.

Ivan Kusalic: Nothing critical. Better interop between Java and Scala collections would be really nice to have.

Better type inference like Hindley-Milner type system that Haskell has is too much to ask for. Luckily now Scala doesn’t need to strive to become a Haskell as there is Frege on JVM.

Daniela Sfregola: I could not think of any.

Julien Tournay: That’s a tough question! Some people would say Scala already has too many features. Maybe something like Shapeless’ Generic (and therefore HLists) should be part of the language.


Don’t forget to read our interview with Martin Odersky, the creator of Scala, about Scala 2.12 and the next releases.

Inline Feedbacks
View all comments