每一个由`scalac`生成的`.class`文件是否也可以通过`javac`获得?

huangapple go评论65阅读模式
英文:

Is every `.class` file generated by `scalac` also obtainable via `javac`?

问题

以下是翻译好的内容:

对于每个由Scala编译器scalac创建的.class文件,理论上是否可能定义一个.java文件,通过javac编译成完全相同的.class文件?

如果不是这样的话,你能否给出一个或多个在Scala中编写的、被编译为JVM字节码的非平凡构造示例,而在Java中没有相应的构造呢?

英文:

Is it true that for every .class file that was created by the Scala compiler scalac, it is theoretically possible to define a .java file that gets compiled, by javac, to exactly this same .class file?

If not, can you give one or more non-trivial examples of constructions in Scala that get compiled to JVM bytecode for which there is no corresponding Java construction?

答案1

得分: 4

每个由Scala编译器scalac创建的.class文件,是否真的都可以有一个相应的.java文件,通过javac编译,最终生成完全相同的.class文件?

不是这样的。

如果不是这样的,请给出一个或多个在Scala中被编译成JVM字节码的结构的例子,这些结构在Java中没有相对应的构造?

class `class`

在Scala中,class是一个合法的类名,但在Java中不是,因此不可能让Java编译器生成一个以class命名的字节码文件。

英文:

> Is it true that for every .class file that was created by the Scala compiler scalac, it is theoretically possible to define a .java file that gets compiled, by javac, to exactly this same .class file?

No.

> If not, can you give one or more examples of constructions in Scala that get compiled to JVM bytecode for which there is no corresponding Java construction?

class `class`

class is a legal name for a class in Scala, but not in Java, so it is impossible to get the Java compiler to generate a bytecode file with a class named class.

答案2

得分: 0

  • 编译Scala时会将ScalaSig(Scala签名)插入字节码中,即一种特定于Scala的注释类型。从Java源代码编译的类文件中将缺少这些“注释”。

  • 编译Java时可以生成在运行时使用Java反射可见的注解(@),而Scala则不行。Scala只能生成在源代码和类文件中可见的注解,但不能在Java反射运行时可见,尽管它们在Scala反射运行时可以可见(因为一些信息写入了ScalaSigs)。

英文:
  • Scala being compiled inserts ScalaSig (scala signatures) into bytecode, i.e. Scala-specific kind of comments. These "comments" will be absent in class files compiled from Java sources.

  • Java being compiled can generate annotations (@) visible with Java reflection at runtime while Scala can't. Scala can only generate annotations visible at sources and class files but not at runtime with Java reflection although they can be visible at runtime with Scala reflection (because some info is written to ScalaSigs).

huangapple
  • 本文由 发表于 2020年9月24日 23:21:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定