为什么行
case Wrapper(rv: Ref[_]) => rv :: state
如果
则不编译def f(r: Ref[_]): List[Ref[_]] = r :: state
编译得很好吗?
有没有办法在保留尽可能多的类型安全性的同时编译这些代码?
这些行来自以下自包含示例:
object Test2 {
trait Entity[T <: Entity[T]]
case class Ref[T <: Entity[T]](t: T)
type State[T <: Entity[T]] = List[Ref[T]]
private var state: State[_] = ???
trait SomeWrapper
case class Wrapper[T <: Entity[T]](at: Ref[T]) extends SomeWrapper
def matcher(w: SomeWrapper): Unit = w match {
case Wrapper(rv: Ref[_]) => rv :: state // does not compile
case _ => ???
}
def f(r: Ref[_]): List[Ref[_]] = r :: state // if this compiles fine, why the above does not compile ?
}
上面的代码给出了以下编译器错误:
Error:(40, 36) type arguments [Any] do not conform to class Ref's type parameter bounds [T <: Test2.Entity[T]]
case Wrapper(rv: Ref[_]) => rv :: state // does not compile
答案 0 :(得分:1)
对我来说看起来像个错误:
w match {
case Wrapper(rv) => (rv: Ref[_]) :: state // Compiles
case Wrapper(rv: Ref[_]) => (rv: Ref[_]) :: state // Compiles
case Wrapper(rv) => rv :: state // Doesn't Compile
case Wrapper(rv: Ref[_]) => rv :: state // Doesn't Compile???
case e: Wrapper[_] => e.at :: state // Compiles
case _ => ???
}