What is Scala runtime, binary compatibility and how are Scala applications deployed?

These are three questions actually, but for me they are tightly connected.

  • What is Scala runtime? Is it just a regular JVM and a classpath to java-lang library? Can I run Scala applications on a plain JVM? Should I always deploy scala-lang.jar along with my own code?

  • Where does that binary compatibility come from? The answer to this question usually sounds like:

Different Scala compilers produce different bytecode.

but this explains not much to me. Does it mean that the 'interface' of the bytecode is changed? Like changing the name of the field containing the instance of companion object, for example. If yes, then why is there no backward compatibility? Is not it possible to add metadata to the bytecode? Scala compiler could use it to change its behaviour/expectations and emit different bytecode for various versions.

  • Does having library cross compiled for several Scala versions imply that using the library from Java will require its own interoperation for each version?

2 answers

  • answered 2017-10-11 10:06 JiriS

    What is Scala runtime?

    Yes, Scala runtime runs on top of JVM and all it needs is supporting scala-lang.jar library and "a plain" JVM of appropriate version. Scala code is compiled to JVM bytecode. You can see a bit more about what Scala compiler does here or you can check compilation phases.

    And yes, when you want to run Scala code on JVM you should always provide scala-lang.jar, but JVM is not strictly necessary to run Scala code. There are projects like Scala native allowing you to run Scala code natively on your platform without JVM or Scala.js allowing you to run it on the top of JavaScript engine.

    Where does that binary (in-)compatibility come from?

    This (in-)compatibility comes from changes in ABI of (not only) Scala standard library (see ABI versioning). There is already a proposal to improve backward binary compatibility. See the details here

  • answered 2017-10-11 10:06 chengpohi

    Scala compile actually is translate Scala source to Java bytecode with including scala-library.

    So when deploy Scala in a plain JVM, need to include scala-library.jar like other dependencies.

    For binary compatibility it means if Scala 2.12.2 is binary compatible with Scala 2.12.1, and project is compiled with Scala 2.12.1, you don't need to recompile project with Scala 2.12.2, it can run directly with Scala 2.12.2 library. and also it means if project has a dependency compiled in Scala 2.12.1, it can be safely used in Scala 2.12.2.

    Reference: http://docs.scala-lang.org/overviews/core/binary-compatibility-of-scala-releases.html