我试图在trait中使用covariant类型参数来构造一个case类,如下所示:
trait MyTrait[+T] {
private case class MyClass(c: T)
}
Run Code Online (Sandbox Code Playgroud)
编译说:
error: covariant type T occurs in contravariant position in type T of value c
Run Code Online (Sandbox Code Playgroud)
然后我尝试了以下但它也没有用:
trait MyTrait[+T] {
private case class MyClass[U <: T](c: U)
}
Run Code Online (Sandbox Code Playgroud)
这次的错误是:
error: covariant type T occurs in contravariant position in type >: Nothing <: T of type U
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么T在这里处于协变位置并建议解决这个问题吗?谢谢!