在我的头发试了几个小时试图获得更复杂的代码编译之后,我把我的困惑简化为这个简单的问题。
我原以为a: => A
是一个归零A
的零参数函数,就像a: () => A
或a: Function0[A]
一样。显然,情况根本不是这样,也是我困惑的根源。
奇怪的是,我无法声明类型为=> A
的本地var / val,我只能将该类型用于函数参数。
// Compiler Error: Expression of type A doesn't conform to expected type () => A
def notFunction0_1[A](a: => A) { val b: Function0[A] = a }
// Compiler Error: Expression of type A doesn't conform to expected type () => A
def notFunction0_2[A](a: => A) { val b: () => A = a }
// Compiles. Also this matches inferred type of simply A.
def justPlainA[A](a: => A) { val b: A = a }
// While I can declare function arguments of type `=> A`, I can't declare local variables of the same type.
def noLocal[A](a: => A) { val b: => A = a }