Compilation of Scala [closed]

2019-08-09 14:56发布

问题:

Can any one please tell me (or denote some source) way of compilation Scala take place.

I have some doubts such as,

  • Within compilation steps, how does Scala compile Java related stuff? Does it uses Java compiler separately?
  • Is the final bytecode of .scala the same as Java's .class file?
  • Due to Sun acquisition by Oracle, does the JVM also belong to Oracle?
  • If some day JVM got proprietary software, would that affect Scala as well?

回答1:

  • Within compilation steps, how does Scala compile Java related stuff?

Scala doesn't compile Java. It knows enough about Java to figure out what the API of the classes will be, but that's all.

  • Does it uses Java compiler separately?

It expects you to compile your java files with the java compiler.

  • Is the final bytecode of .scala the same as Java's .class file?

Yes. Scala produces valid .class files. However, it is not possible to convert that file into Java code, since valid class files allow things that Java doesn't allow or cannot represent.

  • Due to Sun acquisition by Oracle, does the JVM also belong to Oracle?

There's no "the" JVM. There are many existing JVMs, and, in fact, Oracle had their own proprietary JVM. Sun's JVM is now Oracle's, of course.

  • If some day JVM got proprietary software, would that affect Scala as well?

There have always been many proprietary JVMs, one of them being from Oracle.

Let's consider an hypothetical "worst case" scenario: all JVMs in the world become licensed (paid) products or cease to exist. Scala would still be able to run on Dalvik VM, of course, but let's assume it also becomes paid or ceased to exist.

In this case, Scala could still continue to exist, but you'd have to pay to be able to run programs compiled with Scala. In fact, since Scala is a JVM program, you'd have to pay to even use the compiler!

Alternatively, Scala could branch out to some other target platform. There are such efforts in Scala right now, but absolutely no pressure to make them production-grade. If necessary, it would be done.



回答2:

You can look at the compiler source code if you want to, though it would likely be more informative to see Martin Odersky's presentations on the compiler internals first.

In answer to your specific questions:

  • Yes. Early versions of scalac wouldn't do this, and required you to run javac manually first; but scalac can compile Java files using javac where needed since 2.7.2.
  • It's the same format, sure. Depending on exactly what you've done in Scala, it might not result in the exact same JVM operations for an "equivalent" thing, but it is fully conformant with the bytecode specification (which is why it can run on JVMs).
  • Yes, regardless of whether you mean the JVM specification, or the actual vm implementation (JRE download).
  • I'm not sure exactly what you mean by "propertied". But I suspect the answer, in so far as you're asking about it, is "no". Scala compiles down to Java class files, in the standard bytecode format. At runtime, there is no difference between code/classes that were written in Scala, and ones that were written in Java. So anything that would fundamentally prevent Scala code from running on these JVMs would also prevent Java code from running.


标签: java scala jvm