Scala Overhauls Compiler, Aligns with Java 8, in Latest Release

The 2.12 release of Scala, the type-safe programming language for the Java Virtual Machine (JVM), comes with a completely overhauled compiler, which allows the language to fully leverage the VM features in Java 8, the Scala team at Lightbend announced.

In the new compiler, a trait compiles directly to an interface with default methods, which not only improves binary compatibility and Java 8 interoperability, but also enables the alignment of Scala's function encoding with the way Java 8 represents a function as an interface with a single abstract method (SAM). "The FunctionN classes in Scala's standard library are now SAM types," said Adriaan Moors, Scala Team Lead at Lightbend, in a blog post. "In fact, the compiler treats a Java-defined SAM just like our familiar FunctionN, from type checking to code generation."

Ultimately, Moors added, these unifications serve to ensure a smooth experience when interacting with Java-defined higher-order methods (a method that takes a function as an argument), and vice versa, as Scala-defined higher-order methods fit in naturally with Java's approach.

The practical result of these changes is a significant reduction in byte code in many real-world projects, Moors said, the largest of which comes from Scala's use of invokedynamic and Java 8's standard MetaLambdaFactory to avoid emitting an anonymous class for each function literals. "For performance reasons, we've opted to emit slightly more synthetic methods (called forwarders) than strictly necessary in our trait encoding, because they proved to reduce the JVM's startup cost," he said.

This release also ships with a new optimizer; more final methods, including those defined in objects and traits, are now inlined. And closure allocations, dead code, and box/unbox pairs are eliminated more effectively. (Moors credits Netherlands-based software engineer Miguel Garcia for his work on the new optimizer.)

Although Scala 2.11 and 2.12 are mostly source-compatible, the community warned, they are not binary compatible. "This allows us to keep improving the Scala compiler and standard library," the Web site states. "All 2.12.x releases will be fully binary compatible with 2.12.0, in according with the policy we have followed since 2.10."

Scala, which was developed by Lightbend (formerly Typesafe) co-founder Martin Odersky, is a general purpose, multi-paradigm language designed to integrate features of object-oriented programming and functional programming, which emphasizes the evaluation of expressions rather than the execution of commands. Scala runs on the Java Virtual Machine (JVM) and is compatible with existing Java programs. The company also develops Akka, an open source, asynchronous, event-driven middleware implemented in Scala, and the Play Web app framework. Play is a full development and runtime environment billed by the company as "a clean alternative to legacy Enterprise Java stacks" which compiles Java and Scala sources directly and "hot reloads" them into the JVM.

The company's Lightbend Reactive Platform combines several products to support the development of reactive applications on the JVM in both Scala and Java. Conceptualized in the "Reactive Manifesto," which was co-authored by Lightbend CTO and co-founder Jonas Bonér, reactive applications are apps that better meet the "contemporary challenges of software development" in a world in which applications are deployed to everything from mobile devices to cloud-based clusters running thousands of multicore processors.

Scala was among the top "Most Loved" programming languages in Stack Overflow's 2016 survey of 56,033 coders in 173 countries.

About the Author

John K. Waters is the editor in chief of a number of sites, with a focus on high-end development, AI and future tech. He's been writing about cutting-edge technologies and culture of Silicon Valley for more than two decades, and he's written more than a dozen books. He also co-scripted the documentary film Silicon Valley: A 100 Year Renaissance, which aired on PBS.  He can be reached at [email protected].