【发布时间】:2014-06-18 16:57:57
【问题描述】:
我可以理解为什么下面的代码不能编译了:
trait Hello[+A] {
def test[B<:A](x: B)
}
因为:
val child: Hello[String] = new Hello[String] {
def test[B <: String](x: B) = x.substring(1)
}
val parent: Hello[Any] = child
parent.test(123) // child.test can't handle integer
但是我不能很好理解为什么下面的代码不能编译:
trait Hello[+A] {
def test[B<:A]
}
不同的是后者没有参数,我们不能将任何值传递给test方法。
为什么编译器仍然认为它无效?
【问题讨论】:
标签: scala generics contravariance type-systems