object ScalaTrueRing {
def rule = println("To rule them all")
}
this piece of code will be compiled into java byte code, if I decompile it, then the equivalent Java code is similar to this:
public final class JavaTrueRing
{
public static final void rule()
{
ScalaTrueRing..MODULE$.rule();
}
}
/* */ public final class JavaTrueRing$
/* */ implements ScalaObject
/* */ {
/* */ public static final MODULE$;
/* */
/* */ static
/* */ {
/* */ new ();
/* */ }
/* */
/* */ public void rule()
/* */ {
/* 11 */ Predef..MODULE$.println("To rule them all");
/* */ }
/* */
/* */ private JavaTrueRing$()
/* */ {
/* 10 */ MODULE$ = this;
/* */ }
/* */ }
it's compiled into two classes, and if I use Scala.net compiler, it'll be compiled into MSIL code, and the equivalent C# code is like this:
public sealed class ScalaTrueRing
{
public static void rule()
{
ScalaTrueRing$.MODULE$.rule();
}
}
[Symtab]
public sealed class ScalaTrueRing$ : ScalaObject
{
public static ScalaTrueRing$ MODULE$;
public override void rule()
{
Predef$.MODULE$.println("To rule them all");
}
private ScalaTrueRing$()
{
ScalaTrueRing$.MODULE$ = this;
}
static ScalaTrueRing$()
{
new ScalaTrueRing$();
}
}
It's also compiled into two classes.
Why do Scala compilers(the one for Java and the one for .NET) do this? Why does not it just call the println method in the static rule method?
JavaTrueRing$
comes from? – Dishcloth