Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler lies about comparison between Unit and Unit with == always yielding true #9066

Closed
scabug opened this issue Jan 2, 2015 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

scabug commented Jan 2, 2015

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
@scabug
Copy link
Author

scabug commented Jan 2, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9066?orig=1
Reporter: Radek Slupik (rightfold)
Affected Versions: 2.11.4
Duplicates #4572

@scabug
Copy link
Author

scabug commented Jan 3, 2015

@som-snytt said:
null.asInstanceOf[Unit] is the lie. The only Unit value is the Unit value. And that is what that expression nominally evaluates to.

http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#the-null-value

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.

@scabug
Copy link
Author

scabug commented May 18, 2016

@lrytz said:
scala/scala#5176

@scabug scabug closed this as completed May 19, 2016
@scabug scabug added the quickfix label Apr 7, 2017
@scabug scabug added this to the 2.12.0-M5 milestone Apr 7, 2017
@som-snytt
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants