我有一些功能:
def f(x: Int) = x * x
然后我称之为:
var y = 0
f { y += 1; y }
为上述代码生成的字节码如下所示:
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
如果我更改函数def f(x: Int)来表示按名称调用:
def f(x: => Int) = x * x
为同一部分代码生成的字节码如下所示:
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
....
我的问题是:
对于 Call-by-Name,我们对引用进行操作是一个规则,还是取决于编译中的语义分析阶段?