可以在匹配大小写中使用Scala案例对象

时间:2014-10-07 22:06:54

标签: scala

可以在匹配大小写中使用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") }
                 ^

2 个答案:

答案 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") }
}

斯卡拉小提琴:Can a Scala case object be used in a match case