Open mkeskells opened 7 years ago
object Foo {
private[this] var data1 = 7
private var data2 = 7
final var data3 = 7
data1 += 1
data2 += 1
data3 += 1
def foo: Unit = {
var data1 = 7
data1 += 1
this.data1 += 1
this.data2 += 1
}
}
generated bytecode (abbriviated) of
public final class scala/tools/nsc/classpath/Foo$ {
... private I data1 private I data2 private I data3
private data2()I
...
private data2$eq(I)V
...
public final data3()I
...
public final data3$eq(I)V
...
public foo()V
L0
LINENUMBER 14 L0
BIPUSH 7
L1
ISTORE 1
L2
LINENUMBER 15 L2
ILOAD 1
ICONST_1
IADD
ISTORE 1
L3
LINENUMBER 16 L3
ALOAD 0
ALOAD 0
GETFIELD scala/tools/nsc/classpath/Foo$.data1 : I
ICONST_1
IADD
PUTFIELD scala/tools/nsc/classpath/Foo$.data1 : I
L4
LINENUMBER 17 L4
ALOAD 0
ALOAD 0
INVOKESPECIAL scala/tools/nsc/classpath/Foo$.data2 ()I
ICONST1
IADD
INVOKESPECIAL scala/tools/nsc/classpath/Foo$.data2$eq (I)V
L5
RETURN
L6
...
private
compare this with java
public class XX {
int data1 = 7;
XX() {
this.data1 += 1;
}
void foo() {
int data1 = 7;
data1 += 1;
this.data1 += 1;
}
}
generates
I data1
accessing vars generate inefficient bytecode, and non idiomatic code WRT java equiv
Not sure if this is inefficient machine code though, but at least it is increating the classfile size
comparing with java -