【发布时间】:2014-04-29 15:57:16
【问题描述】:
Scala 代码:
class Cat[T] {
def meow[K <: T] = ""
}
class Cat[-T] {
def meow[K <: T] = ""
}
它们可以被编译。
但是当T为协方差时,以下代码无法编译:
class Cat[+T] {
def meow[K <: T] = ""
}
编译器打印:
error: covariant type T occurs in contravariant position in type <: T of type K
def meow[K <: T] = ""
^
<:右边的类型一定不能协变?为什么?
【问题讨论】:
-
T in “K <: T” can't be covariance仅在负数位置可用。
标签: scala type-systems type-bounds