【发布时间】:2014-07-01 21:31:31
【问题描述】:
我发现自己不止一次写过以下丑陋的模式:
class Something[A, B](implicit ev: A =:= B = null) {
...
def doStuff {
if (ev == null) ... // know that A is not the same as B
else ... // safely assume A is equal to B
}
}
更糟糕的是,当ev != null时,我有时会写诸如someB.asInstanceOf[A]之类的异端。
【问题讨论】:
-
为什么不直接对待它,就像对待
null的可能性在其他任何地方(即Option(ev))一样?