【发布时间】:2020-03-05 21:19:21
【问题描述】:
为什么当类型参数来自封闭方法而不是封闭类时,模式匹配的工作方式不同?例如,
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
}
}
}
【问题讨论】:
标签: scala pattern-matching type-inference type-parameter