可以在匹配大小写中使用Scala案例对象吗?
E.g。这不工作:
abstract class A
case object B extends A
object something {
val b = B
b match { case _:B => println("success") }
}
not found: type B
b match { case _:B => println("success") }
^
答案 0 :(得分:12)
您需要指定B.type
:
object something {
val b = B
b match { case _:B.type => println("success") }
}
答案 1 :(得分:7)
abstract class A
case object B extends A
object something {
val b = B
b match { case B => println("success") }
}