You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When comparing two values of type Unit with == the compiler warns that said comparison will always yield true. However, this is not the case when exactly one of the arguments is null:
scala> null.asInstanceOf[Unit] == {}
<console>:8: warning: comparing values of types Unit and Unit using `==' will always yield true
null.asInstanceOf[Unit] == {}
^
res0: Boolean = false
The text was updated successfully, but these errors were encountered:
But #602 is the canonical issue and #4572 turns up the same alley with Unit. BoxedUnit is like the wrapped gifts under the tree at the mall. Nobody ever unboxes them, not even to check for a lump of coal.
Maybe if not casting, such as in an Any context, do null conversion to BoxedUnit.
scala> def fromNull[A]: A = null.asInstanceOf[A]
fromNull: [A]=> A
scala> null.asInstanceOf[Unit] == fromNull[Unit]
^
warning: comparing values of types Unit and Unit using `==` will always yield true
res0: Boolean = false
scala> println(fromNull[Unit])
null
scala> def f[A]: A = new Object().asInstanceOf[A]
f: [A]=> A
scala> () == f[Unit]
^
warning: comparing values of types Unit and Unit using `==` will always yield true
res0: Boolean = false
scala> 42 == f[Int]
java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.lang.Integer (java.lang.Object and java.lang.Integer are in module java.base of loader 'bootstrap')
at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:100)
... 28 elided
When comparing two values of type
Unit
with==
the compiler warns that said comparison will always yieldtrue
. However, this is not the case when exactly one of the arguments isnull
:The text was updated successfully, but these errors were encountered: