Search code examples
javascalaprogramming-languagesreferenceruntime

What's the purpose of these Java files in scala.runtime?


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?


Solution

  • Just a guess, too: there is no Scala construct that compiles to a public field — public vars 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.