|
| 1 | +# Experimental Backend in Scala 2.11 |
| 2 | + |
| 3 | +Scala 2.11 ships with an experimental backend named "GenBCode", originally implemented by [@magarciaEPFL](https://github.com/magarciaEPFL). |
| 4 | +This backend will become the new default in Scala 2.12. |
| 5 | +Given that the new backend is experimental in Scala 2.11, it can be modified and extended without compatibility concerns. |
| 6 | +Most of the work towards the Scala 2.12 backend happens in the 2.11 branch and flows into 2.11.x minor releases. |
| 7 | + |
| 8 | +This gives you the chance to preview pieces of Scala 2.12 just by enabling some compiler flags in 2.11! |
| 9 | + |
| 10 | +## Enabling GenBCode |
| 11 | + |
| 12 | +`-Ybackend:GenBCode` enables the new backend. |
| 13 | + |
| 14 | +GenBCode without any further compiler flags is a drop-in replacement for the current backend ("GenASM"). |
| 15 | +The generated bytecode is binary compatible, altough not exactly the same; some instruction sequences may differ. |
| 16 | +If you find a binary incompatibility, please [report an issue](https://issues.scala-lang.org/secure/Dashboard.jspa). |
| 17 | + |
| 18 | +## Delambdafy-Method |
| 19 | + |
| 20 | +`-Ydelambdafy:method` is an experimental option in Scala 2.11 to alter the translation of lambdas, implemented by [@JamesIry](https://github.com/JamesIry). |
| 21 | + |
| 22 | +Traditionally, the anonymous class generated for a lambda contains the lambda body in the `apply` method of the class. |
| 23 | +When using delambdafy-method, a method containing the lambda body is generated for each lambda, and the anonymous class delegates to this method. |
| 24 | + |
| 25 | +Delambdafy-method is a requiement for emitting lambdas in Java 8 style, as explained in detail in [this Gist by @retronym](https://gist.github.com/retronym/0178c212e4bacffed568). |
| 26 | + |
| 27 | +## Target jvm-1.8 |
| 28 | + |
| 29 | +`-target:jvm-1.8` instructs the Scala compiler to generate classfiles of version 52 (the default is `jvm-1.6`, generating classfiles of version 50). |
| 30 | + |
| 31 | +This option is one of the requirements to enable emitting lambdas in Java 8 style. |
| 32 | + |
| 33 | +## Scala-Java8-Compat |
| 34 | + |
| 35 | +The [`scala-java8-compat` library](https://github.com/scala/scala-java8-compat) defines subtypes of Scala's `FunctionN` that have a single abstract method. |
| 36 | + |
| 37 | +The `FunctionN` traits in Scala are compiled to interface classfiles with multiple abstract methods (`compose`, `apply`, `andThen`, specialized variants of `apply`). |
| 38 | +The compat library compiles to Java 8 bytecode and makes use of [default methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) for all methods but `apply`. |
| 39 | + |
| 40 | +## Emitting Java 8 Style Lambdas |
| 41 | + |
| 42 | +The Scala 2.11.7 compiler emits lambdas in Java 8 style (using `invokedynamic` and LambdaMetaFactory) if the following conditions are met: |
| 43 | + - the compiler is running on Java 8 |
| 44 | + - `-Ybackend:GenBCode` is enabled |
| 45 | + - `-Ydelambdafy:method` is enabled |
| 46 | + - `-target:jvm-1.8` is enabled |
| 47 | + - the scala-java8-compat library is on the compiler's classpath |
| 48 | + |
| 49 | +In an SBT project, this can be achieved using the following settins: |
| 50 | + |
| 51 | + libraryDependencies += "org.scala-lang.modules" %% "scala-java8-compat" % "0.5.0" |
| 52 | + |
| 53 | + scalacOptions ++= List("-Ybackend:GenBCode", "-Ydelambdafy:method", "-target:jvm-1.8") |
| 54 | + |
| 55 | +An sbt project with these options can be found here: https://github.com/lrytz/experimental-backend-2.11 |
| 56 | + |
| 57 | +[The Gist by @retronym](https://gist.github.com/retronym/0178c212e4bacffed568) explains all the glorious details required to generate Java 8 style lambdas from Scala code. |
| 58 | + |
| 59 | +## New Optimizer |
| 60 | + |
| 61 | +`-Yopt:l:classpath` enables the new optimizer. |
| 62 | + |
| 63 | +As part of the new backend for Scala 2.12 we are developing a new inliner and bytecode optimizer. |
| 64 | +The inliner is already available in Scala 2.11.7 and can be enabled using the `-Yopt` compiler option. |
| 65 | +Check `-Yopt:help` to see the available optimizations. |
| 66 | + |
| 67 | +If you encounter a bug, please file an issue on this bugracker: https://github.com/scala-opt/scala/issues |
0 commit comments