事实证明,在模式匹配中使用单例对象依赖于他们的equals
方法:
trait Foo
object A extends Foo {
override def equals(other: Any): Boolean = false
}
object B extends Foo {
override def equals(other: Any): Boolean = true
}
val a: Foo = A
a match {
case A => "is A"
case B => "is B"
case _ => "unknown"
}
// evaluates to "is B"
为什么不简单地使用引用相等(eq
)?