当类型参数来自封闭方法而不是封闭类时,为什么模式匹配的工作方式有所不同?例如,
trait Base[T]
case class Derived(v: Int) extends Base[Int]
class Test[A] {
def method(arg: Base[A]) = {
arg match {
case Derived(_) => 42
}
}
}
给出错误
constructor cannot be instantiated to expected type;
found : A$A87.this.Derived
required: A$A87.this.Base[A]
case Derived(_) => 42
^
虽然它成功地编译A
为方法类型参数
class Test {
def method[A](arg: Base[A]) = {
arg match {
case Derived(_) => 42
}
}
}