不一致的null相等性检查scala 2.11.7 [英] Inconsistent null equality check scala 2.11.7
问题描述
Scala 2.12.6中不再存在此问题
This issue no longer exists in Scala 2.12.6
原始问题(针对Scala 2.11.7):
Original question (for Scala 2.11.7):
为什么这么奇怪的警告?
Why so strange warning?
scala> null.asInstanceOf[Double]
res0: Double = 0.0
scala> null.asInstanceOf[Double] == null
<console>:11: warning: comparing values of types
Double and Null using `==' will always yield !!!!false!!!!
null.asInstanceOf[Double] == null
^
res1: Boolean = true //!!!!
scala> 0.0 == null
<console>:11: warning: comparing values of types Double and Null using `==' will always yield false
0.0 == null
^
res2: Boolean = false
scala> null.asInstanceOf[Double] == 0.0
res6: Boolean = true
scala> val a = null.asInstanceOf[Double]
a: Double = 0.0
scala> a == null
<console>:12: warning: comparing values of types Double and Null using `==' will always yield false
a == null
^
res7: Boolean = false
P.S. Int
和Long
P.S.2这不是重复的-这里的问题是,无论asInstanceOf
(如您从我的回答中看到的那样),根本不会发生装箱+警告消息不一致
P.S.2 It's not a duplicate - the problem here is that boxing doesn't happen at all regardless of asInstanceOf
(as you can see from my answer) + the warning message is inconsistent
推荐答案
此 issue 在Scala 2.12.6中不再存在.请参见拉动请求及其说明.
This issue no longer exists in Scala 2.12.6. See pull-request with explanation.
原始答案(针对Scala 2.11.7):
Original answer (for Scala 2.11.7):
null.asInstanceOf[Double] == null
编译为:
aconst_null
ifnonnull
val
版本可编译为:
aconst_null
invokestatic unboxToDouble
putfield
aload_0
invokevirtual а
invokestatic boxToDouble
ifnonnull
所以编译器只是忘了在第一种情况下添加unbox/box
So compiler just forgets to add unbox/box in the first case
这篇关于不一致的null相等性检查scala 2.11.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!