c# - Why does Scala compiler for .NET ignore the meaning of val? -


i playing around scala. , found 3 interesting things(the title third 1 ).

1 local variable declared val not interpreted final.

class howarevarandvalimplementedinscala {   var v1 = 123   val v2 = 456    def method1() = {     var v3 = 123     val v4 = 456     println(v3 + v4)   } } 

if compile above scala code bytecode , decompile java, looks this:

public class howarevarandvalimplementedinscala {   private int v1 = 123;   private final int v2 = 456;    public int v1()   {     return this.v1;   }    public void v1_$eq(int x$1) { this.v1 = x$1; }    public int v2() { return this.v2; }    public void method1() {     int v3 = 123;     int v4 = 456;     predef..module$.println(boxesruntime.boxtointeger(v3 + v4));   } } 

we can see v2 final, v4 not, why?

2 scala compiler .net adds override keyword lot of(if not all) public instance methods

if compile scala code shown above cil , decompile c#, it's this:

public class howarevarandvalimplementedinscala : scalaobject {   private int v1;   private int v2;    public override int v1()   {     return this.v1;   }    public override void v1_$eq(int x$1)   {     this.v1 = x$1;   }    public override int v2()   {     return this.v2;   }    public override void method1()   {     int v3 = 123;     int v4 = 456;     predef$.module$.println(v3 + v4);   }    public howarevarandvalimplementedinscala()   {     this.v1 = 123;     this.v2 = 456;   } } 

all public instance methods(exclude constructor) marked override, why? necessary?

3 scala compiler .net looses meaning of val

in above c# code, can see v2 normal field, while in java couter part, v2 marked final, shouldn't v2 marked readonly scala compiler .net?(a bug?)

at bytecode level, final doesn't exist local variables. in fact concept of local variables doesn't exist either. marking local variable final purely compile time checking. since information isn't present in classfile, decompiler has no way of guessing it.

as second 2 questions, i'm not familiar cil bytecode, if had guess i'd there's no reason not add override, , readonly might have different semantics.

edit: after looking through cil specification, here's i've found.

the cil equivalent of java's final flag fields initonly, appears have same semantics. it's not clear why scala compiler doesn't emit this. perhaps didn't around it? or perhaps .net decompiler used didn't reflect this. if want see compiler generates, you're best off looking @ bytecode directly.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -