如何和为什么“VAL”和“案例”影响类型系统? (尤其是方差)逆变和Val
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> class E[-A]
defined class E
scala> class F[-A](val f: E[A] => Unit)
<console>:6: error: contravariant type A occurs in covariant position in type => (E[A]) => Unit of value f
class F[-A](val f: E[A] => Unit)
^
scala> case class C[-A](f: E[A] => Unit)
<console>:6: error: contravariant type A occurs in covariant position in type => (E[A]) => Unit of value f
case class C[-A](f: E[A] => Unit)
scala> class F[-A](f: E[A] => Unit)
defined class F
所以这意味着我的例子中的决定因素是'val'和'case'为构造函数参数生成公共成员? – ladrl 2011-02-24 07:46:14
从打字的角度来看,你可以认为你的代码是“E类[-A] {def f:A = ...},这使得A处于协变位置。” – 2011-02-24 17:25:56
@ladrl这是正确的。 – 2011-02-24 17:58:55