When two versions of Scala are binary compatible, it is safe to compile your project on one Scala version and link against another Scala version at run time. Safe run-time linkage (only!) means that the JVM does not throw a (subclass of)
LinkageError when executing your program in the mixed scenario, assuming that none arise when compiling and running on the same version of Scala. Concretely, this means you may have external dependencies on your run-time classpath that use a different version of Scala than the one you’re compiling with, as long as they’re binary compatible. In other words, separate compilation on different binary compatible versions does not introduce problems compared to compiling and running everything on the same version of Scala.
We check binary compatibility automatically with MiMa. We strive to maintain a similar invariant for the
behavior (as opposed to just linkage) of the standard library, but this is not checked mechanically (Scala is not a proof assistant so this is out of reach for its type system).
Forwards and Back
We distinguish forwards and backwards compatibility (think of these as properties of a sequence of versions, not of an individual version). Maintaining backwards compatibility means code compiled on an older version will link with code compiled with newer ones. Forwards compatibility allows you to compile on new versions and run on older ones.
Thus, backwards compatibility precludes the removal of (non-private) methods, as older versions could call them, not knowing they would be removed, whereas forwards compatibility disallows adding new (non-private) methods, because newer programs may come to depend on them, which would prevent them from running on older versions (private methods are exempted here as well, as their definition and call sites must be in the same compilation unit).
These are strict constraints, but they have worked well for us since Scala 2.10.x. They didn’t stop us from fixing large numbers of issues in minor releases. The advantages are clear, so we will maintain this policy for future Scala major releases.
Note that so far we’ve only talked about the jars generated by scalac for the standard library and reflection. Our policies do not extend to the meta-issue: ensuring binary compatibility for bytecode generated from identical sources, by different version of scalac? (The same problem exists for compiling on different JDKs.) While we strive to achieve this, it’s not something we can test in general. Notable examples where we know meta-binary compatibility is hard to achieve: specialisation and the optimizer.
In short, we recommend that library authors use to MiMa to verify compatibility of minor versions before releasing. Compiling identical sources with different versions of the scala compiler (or on different JVM versions!) could result in binary incompatible bytecode. This is rare, and we try to avoid it, but we can’t guarantee it will never happen.
We guarantee forwards and backwards compatibility of the
"org.scala-lang" % "scala-library" % "2.N.x" and
"org.scala-lang" % "scala-reflect" % "2.N.x" artifacts, except for
scala.reflect.iopackages, as scala-reflect is still experimental, and
scala.runtimepackage, which contains classes used by generated code at runtime.
We also strongly discourage relying on the stability of
scala.reflect.runtime, though we will only break compatibility for severe bugs here.
Note that we will only enforce backwards binary compatibility for modules (artifacts under the groupId
org.scala-lang.modules). As they are opt-in, it’s less of a burden to require having the latest version on the classpath. (Without forward compatibility, the latest version of the artifact must be on the run-time classpath to avoid linkage errors.)
Finally, from Scala 2.11 until Scala 2.13.0-M1,
scala-library-all aggregates all modules that constitute a Scala release. Note that this means it does not provide forward binary compatibility, whereas the core
scala-library artifact does. We consider the versions of the modules that
"scala-library-all" % "2.N.x" depends on to be the canonical ones, that are part of the official Scala distribution. (The distribution itself is defined by the
scala-dist maven artifact.)