これがコンパイルに失敗する (または動作する) のはなぜですか?
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?")
}