|
| 1 | +--- |
| 2 | +layout: news |
| 3 | +post-type: announcement |
| 4 | +permalink: /news/2.12.0-M5 |
| 5 | +title: "Scala 2.12.0-M5 is now available!" |
| 6 | +--- |
| 7 | +We are happy to announce the availability of Scala 2.12.0-M5! |
| 8 | + |
| 9 | +Notable changes in M5 |
| 10 | +([see here](https://github.com/scala/scala/pulls?utf8=%E2%9C%93&q=milestone%3A2.12.0-M5%20label%3Arelease-notes) |
| 11 | +for a more extensive list): |
| 12 | + - [#5251](https://github.com/scala/scala/pull/5251): concrete trait methods are compiled to a |
| 13 | + static method (in the interface classfile) containing the actual implementation, and a default |
| 14 | + method forwards that to the static one. |
| 15 | + - [#5085](https://github.com/scala/scala/pull/5085): classes extending traits no longer get mixin |
| 16 | + forwarders (in most cases), the JVM picks the correct default method. |
| 17 | + - [#5102](https://github.com/scala/scala/pull/5102) adds partial unification of type constructors |
| 18 | + (behind `-Ypartial-unification`) and fixes SI-2712. Thanks @milessabin! |
| 19 | + - The optimizer now listens to `-opt` (instead of `-Yopt`) |
| 20 | + - [SI-9390](https://issues.scala-lang.org/browse/SI-9390): local methods that don't capture `this` |
| 21 | + are emitted as static to prevent capturing the outer instance in lambdas. |
| 22 | + |
| 23 | +*Note*: #5085 introduces a performance regression, it causes the Scala compiler to run 20% slower. |
| 24 | +Any Scala program that uses concrete trait methods might see a slowdown. The most likely |
| 25 | +cause is that the JVM does not perform "class hierarchy analysis" (CHA) for methods |
| 26 | +defined in interfaces, including default methods, as noted in |
| 27 | +[JDK bug 8036580](https://bugs.openjdk.java.net/browse/JDK-8036580). This prevents the JIT from |
| 28 | +performing certain (important) optimizations. Most likely we will have to revert the change to fix |
| 29 | +the issue, but we are still investigating the details. The 2.12 release notes will clarify the |
| 30 | +impact of changes to traits on binary compatibility. |
| 31 | + |
| 32 | +In total, we merged [96 pull requests](https://github.com/scala/scala/pulls?q=is%3Apr+is%3Amerged+milestone%3A2.12.0-M5), of which [9 are by new contributors](https://github.com/scala/scala/pulls?utf8=%E2%9C%93&q=is%3Apr%20is%3Amerged%20milestone%3A2.12.0-M5%20label%3Awelcome) -- welcome! |
| 33 | +This milestone resolves [49 JIRA tickets](https://issues.scala-lang.org/issues/?jql=project%20%3D%20SI%20AND%20status%20%3D%20CLOSED%20AND%20resolution%20%3D%20Fixed%20AND%20fixVersion%20%3D%20%22Scala%202.12.0-M5%22%20ORDER%20BY%20component%20ASC%2C%20priority%20DESC). |
| 34 | + |
| 35 | +As usual for milestones, 2.12.0-M5 is not binary compatible with any other Scala release, including other 2.12 milestones. Scala 2.12 requires a Java 8 runtime. |
| 36 | + |
| 37 | +## Scala 2.12 |
| 38 | + |
| 39 | +Scala 2.12 is all about making optimal use of Java 8's new features. Traits ([#5003](https://github.com/scala/scala/pull/5003)) and functions are compiled to their Java 8 equivalents, and we treat Single Abstract Method types and Scala's builtin function types uniformly from type checking to the back end ([#4971](https://github.com/scala/scala/pull/4971)). We also use `invokedynamic` for a more natural encoding of other language features ([#4896](https://github.com/scala/scala/pull/4896)). We've standardized on the GenBCode back end ([#4814](https://github.com/scala/scala/pull/4814), [#4838](https://github.com/scala/scala/pull/4838)) and the flat classpath implementation is now the default ([#5057](https://github.com/scala/scala/pull/5057)). The optimizer has been completely overhauled for 2.12. |
| 40 | + |
| 41 | +Except for the breaking changes listed below, code that compiles on 2.11.x without deprecation warnings should compile on 2.12.x too, unless you use experimental APIs such as reflection. If you find incompatibilities, please [file an issue](https://issues.scala-lang.org). |
| 42 | + |
| 43 | +### New features |
| 44 | + |
| 45 | +Since M4, we consider 2.12.x feature complete. |
| 46 | +For RC1, we will try to remain binary compatible with M5, and we won't risk regressions except for the most critical bugs. |
| 47 | + |
| 48 | +#### Trait compiles to an interface |
| 49 | +With Java 8 allowing concrete methods in interfaces, Scala 2.12 is able to compile a trait to a single interface. Before, a trait was represented as a class that held the method implementations and an interface. Note that the compiler still has quite a bit of magic to perform behind the scenes, so that care must be taken if a trait is meant to be implemented in Java. (Briefly, if a trait does any of the following its subclasses require synthetic code: defining fields, calling super, initializer statements in the body, extending a class, relying on linearization to find implementations in the right super trait.) |
| 50 | + |
| 51 | +#### Java 8-style lambdas |
| 52 | + |
| 53 | +Scala 2.12 emits closures in the same style as Java 8, whether they target a FunctionN class from the standard library or a user-defined Single Abstract Method type. The type checker accepts a function literal as a valid expression for either kind of "function-like" type (built-in or SAM). This improves the experience of using libraries written for Java 8 in Scala. |
| 54 | + |
| 55 | +For each lambda the compiler generates a method containing the lambda body, and emits an `invokedynamic` that will spin up a lightweight class for this closure using the JDK's `LambdaMetaFactory`. |
| 56 | + |
| 57 | +Compared to Scala 2.11, the new scheme has the advantage that, in most cases, the compiler does not need to generate an anonymous class for each closure. This leads to significantly smaller JAR files. |
| 58 | + |
| 59 | +#### New back end |
| 60 | + |
| 61 | +Scala 2.12 standardizes on the "GenBCode" back end, which emits code more quickly because it directly generates ASM bytecode from Scala compiler trees, while the previous back end used an intermediate representation called "ICode". The old back ends (GenASM and GenIcode) have been removed ([#4814](https://github.com/scala/scala/pull/4814), [#4838](https://github.com/scala/scala/pull/4838)). |
| 62 | + |
| 63 | + |
| 64 | +#### New bytecode optimizer |
| 65 | + |
| 66 | +The GenBCode back end includes a new inliner and bytecode optimizer. |
| 67 | +The optimizer is enabled using the `-opt:l:classpath` compiler option. |
| 68 | +Check `-opt:help` to see the full list of available options for the optimizer. |
| 69 | + |
| 70 | +The following optimizations are available: |
| 71 | + |
| 72 | +* Inlining final methods, including methods defined in objects and final methods defined in traits |
| 73 | +* If a closure is allocated and invoked within the same method, the closure invocation is replaced by an invocations of the corresponding lambda body method |
| 74 | +* Dead code elimination and a small number of cleanup optimizations |
| 75 | +* Box/unbox elimination [#4858](https://github.com/scala/scala/pull/4858) |
| 76 | + |
| 77 | +The work on the new optimizer is still ongoing. You can track it in the [scala-dev repository issue tracker](https://github.com/scala/scala-dev/labels/t%3Aoptimizer). |
| 78 | + |
| 79 | + |
| 80 | +### Breaking changes |
| 81 | + |
| 82 | +#### SAM types |
| 83 | +As of [#4971](https://github.com/scala/scala/pull/4971), we treat Single Abstract Method types in the same way as our built-in FunctionN classes. This means overloading resolution has more contenders to choose from, making type inference less effective. Here's an example: |
| 84 | + |
| 85 | +```scala |
| 86 | +class C[V] { |
| 87 | + def sort(cmp: java.util.Comparator[V]): C[V] = ??? |
| 88 | + def sort(cmp: (V, V) => Int): C[V] = sort( |
| 89 | + new java.util.Comparator[V] { |
| 90 | + def compare(a: V, b: V): Int = cmp(a, b) |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +(new C[Int]) sort (_ - _) // error |
| 95 | +(new C[Int]) sort ((_ - _): java.util.Comparator[Int]) // ok |
| 96 | +(new C[Int]) sort ((a: Int, b: Int) => a - b) // ok |
| 97 | +``` |
| 98 | + |
| 99 | +The first attempt fails because the type checker cannot infer the types for `_ - _`'s arguments anymore. |
| 100 | +Type inference in this scenario only works when we can narrow the overloads down to one before type checking the arguments the methods are applied to. When a function is passed as an argument to an overloaded method, we do this by considering the "shape" of the function (essentially, its arity). Now that `Comparator[?]` and `(?, ?) => ?` are both considered functions of arity two, our clever scheme breaks down and the programmer must either select an overload (second application) or make the argument types explicit (last application, which resolves to the `Function2` overload). |
| 101 | + |
| 102 | +Finally, implicit conversion of SAM types to Function types won't kick in anymore, since the compiler does this conversion itself first: |
| 103 | + |
| 104 | +```scala |
| 105 | +trait MySam { def apply(x: Int): String } |
| 106 | + |
| 107 | +implicit def unused(fun: Int => String): MySam |
| 108 | + = new MySam { def apply(x: Int) = fun(x) } |
| 109 | + |
| 110 | +// uses sam conversion, not the `unused` implicit |
| 111 | +val sammy: MySam = _.toString |
| 112 | +``` |
| 113 | + |
| 114 | +#### Changed syntax trees (affects macro and compiler plugin authors) |
| 115 | + |
| 116 | +PR [#4794](https://github.com/scala/scala/pull/4749) changed the syntax trees for selections of statically accessible symbols. For example, a selection of `Predef` no longer has the shape `q"scala.this.Predef"` but simply `q"scala.Predef"`. Macros and compiler plugins matching on the old tree shape need to be adjusted. |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +## Binary compatibility |
| 122 | + |
| 123 | +Since Scala 2.11, minor releases of Scala are binary compatible with each other. |
| 124 | +Scala 2.12 will continue this tradition: every 2.12.x release will be binary compatible with 2.12.0. |
| 125 | +Milestone releases and release candidates, however, are **not** binary compatible with any other release. |
| 126 | + |
| 127 | +Scala 2.12 is not and will not be binary compatible with the 2.11.x series. This allows us to keep improving the Scala compiler and standard library. We are working with the community to ensure that core projects in the Scala eco-system become available for 2.12. Please refer to this growing [list of libraries and frameworks](https://github.com/scala/make-release-notes/blob/2.12.x/projects-2.12.md). |
| 128 | + |
| 129 | +The [Scala 2.11.1 release notes](http://scala-lang.org/news/2.11.1) explain in more detail on how binary compatibility works in Scala. The same policies apply to 2.12 as well. |
| 130 | + |
| 131 | + |
| 132 | +## Contributors |
| 133 | + |
| 134 | +A big thank you to everyone who's helped improve Scala by reporting bugs, improving our documentation, spreading kindness in mailing lists and other public fora, and submitting and reviewing pull requests! You are all magnificent. |
| 135 | + |
| 136 | +According to `git shortlog -sn --no-merges v2.12.0-M4..v2.12.0-M5`, the following contributors helped to realize this milestone: Lukas Rytz, A. P. Marki, Jason Zaugg, Sébastien Doeraene, Adriaan Moors, Performant Data LLC, Simon Ochsenreither, Janek Bogucki, Miles Sabin, Felix Mulder, Stefan Zeiger, Rui Gonçalves, Raphael Jolly, Arno den Hartog, Viktor Klang, Olli Helenius, Kenji Yoshida, Steve Robinson, Taras Boiko, af, chrisokasaki, peterz, sh0hei, Łukasz Gieroń, Antoine Gourlay, Ben Hutchison, CodingTwinky, Dale Wijnand, Iulian Dragos, Jakob Odersky, Jens, Krzysztof Romanowski, Martijn Hoekstra, Mike Pheasant, Nafer Sanabria, Nicolas Stucki, Performant Data, Ruslan Sennov, Seth Tisue, Shixiong Zhu. Thank you! |
| 137 | + |
| 138 | +## Release notes |
| 139 | + |
| 140 | +Improvements to these release notes [are welcome!](https://github.com/scala/make-release-notes/blob/2.12.x/hand-written.md) |
| 141 | + |
| 142 | +## Obtaining Scala |
| 143 | + |
| 144 | +* Download a distribution from [scala-lang.org](http://scala-lang.org/download/2.12.0-M5.html) |
| 145 | +* Bump the `scalaVersion` setting in your SBT-based project |
| 146 | +* Obtain JARs via [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.scala-lang%22%20AND%20v%3A%222.12.0-M5%22) |
0 commit comments