scala.runtime 中这些 Java 文件的用途是什么?
Scala 的源代码树中有一些 *.java 文件,位于 scala.runtime 目录。
这些文件看起来非常简单,例如 DoubleRef.java
看起来像这样:
package scala.runtime;
public class DoubleRef implements java.io.Serializable {
private static final long serialVersionUID = 8304402127373655534L;
public double elem;
public DoubleRef(double elem) { this.elem = elem; }
public String toString() { return java.lang.Double.toString(elem); }
}
Is there any Reason Why those classes can't be Define in Scala?
There are a few *.java files in the source tree of Scala in the scala.runtime directory.
Those files seem to be very simple, e. g. DoubleRef.java
looks like this:
package scala.runtime;
public class DoubleRef implements java.io.Serializable {
private static final long serialVersionUID = 8304402127373655534L;
public double elem;
public DoubleRef(double elem) { this.elem = elem; }
public String toString() { return java.lang.Double.toString(elem); }
}
Is there any reason why those classes can't be defined in Scala?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也只是猜测:不存在编译为公共字段的 Scala 构造 — Scala 中的公共
var
编译为私有字段以及公共访问器和修改器。正如我想象的那样,这些*Ref
类被广泛使用,这可能是一种优化,以避免频繁的方法调用并用直接字段访问替换它们。Just a guess, too: there is no Scala construct that compiles to a public field — public
var
s in Scala compile to a private field and to a public accessor and mutator. As I imagine that those*Ref
classes are used extensively, this could be an optimization to avoid frequent method calls and replace them with direct field access.