What is the reason for nesting class files under 'scala' directory?

Viewed 78

SBT puts compiled scala files in folder target/scala-<scalaVersion>.

As far as I understand class files contain JVM bytecode. They are language agnostic.

What is then the reason for the folder with the name of the language? I'd expect that class filed of both Scala and Java go to the same directory.

2 Answers

The compiled bytecode is language-agnostic, but of course, the code inside that bytecode may call out to libraries, including the Scala standard library. The compiler may also have generated calls into the Scala runtime.

The Scala runtime may change between versions, for example, the way lambdas are represented has changed completely in 2.12. A program compiled for Scala 2.12 will not work with a Scala 2.11 runtime.

So, while JVM bytecode is language agnostic, you still need the matching runtime.

Note that this is no different from any other language. If you compile C to native code, you still can't run your code, unless you have the proper C runtime in place.

SBT supports building the same project with multiple Scala versions (and then producing separate artifacts for them, etc.). These files will naturally be in the same path relative to their target directory (since this path is determined by the full class name). So different Scala versions need different target directories to avoid conflict.

Related