为什么这无法编译(或工作?):
case class A(x: Int)
class B extends A(5)
(new B) match {
case A(_) => println("found A")
case _ => println("something else happened?")
}
编译器错误是:
constructor cannot be instantiated to expected type; found : blevins.example.App.A required: blevins.example.App.B
请注意,这将按预期编译和运行:
(new B) match {
case a: A => println("found A")
case _ => println("something else happened?")
}
附录
仅供参考,编译并运行良好:
class A(val x: Int)
object A {
def unapply(a: A) = Some(a.x)
}
class B extends A(5)
(new B) match {
case A(i) => println("found A")
case _ => println("something else happened?")
}