I have some function:
def f(x: Int) = x * x
and then I call it:
var y = 0
f { y += 1; y }
Bytecode generated for above code looks like:
0: iconst_0
1: istore_1
2: aload_0
3: iload_1
4: iconst_1
5: iadd
6: istore_1
7: iload_1
8: invokevirtual #18 // Method f:(I)I
11: pop
12: return
If I change function def f(x: Int) to represent Call-by-Name:
def f(x: => Int) = x * x
generated bytecode for the same part of code looks like:
0: new #24 // class scala/runtime/IntRef
3: dup
4: iconst_0
5: invokespecial #28 // Method scala/runtime/IntRef."<init>":(I)V
8: astore_1
9: aload_0
....
My question is:
Is it a rule that for Call-by-Name we oparate on references or it depends on semantic analysis phase in compilation?