爲什麼這個編譯失敗(或工作):匹配的子類在斯卡拉
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?")
}
我認爲這是目前在模式匹配上開放的漏洞之一。 – 2010-01-13 23:27:14