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
Incorrect compiler warning about null comparisons with primitive types #602
Comments
Imported From: https://issues.scala-lang.org/browse/SI-602?orig=1 |
@mcdirmid said: |
@blair said: The HashMap has to be parameterized with a reference type, so
I tested checking out trunk and changing that 0 to 123 and getting I think in this case you would want to Scala be smart enough not to The workaround is to explicitly use java.lang.Integer's, at least for the scala> val foo = new java.util.HashMap[Int, Integer]
foo: java.util.HashMap[Int,Integer] = {}
scala> foo.get(0) == null
res0: Boolean = true As an aside, I don't like the idea of null Int's, nor any of the other |
@DRMacIver said:
Because the actual example I need this for isn't a HashMap, it's an IdentityHashMap which doesn't have a wrapper. It just happened to be easier to use a HashMap for the example. |
@DRMacIver said:
I'm fine with fixing the semantics as long as doing so doesn't block the use case. But fixing the semantics and making a significant number of Java types unusable as a result is a Really Bad Idea.
Sure.
I tried exactly this. It generates a deprecation warning in the compiler. |
@odersky said: |
@dubochet said: |
@odersky said: |
@paulp said: |
@lrytz said: scala> def f(x: Any) = "" + x
f: (x: Any)String
scala> f(null.asInstanceOf[Int])
res0: String = null
scala> f({ val a = null.asInstanceOf[Int]; a })
res1: String = 0 |
The following code warns that the expression will always evaluate to false. The expression promptly proceeds to evaluate to true.
This needs either fixed semantics or a fixed warning. I'd prefer the latter, as "fixing" the semantics would make Java collections very hard to use correctly on Scala primitives (I was very annoyed by the fact that I couldn't usefully use them on Scala primitives until I discovered the compiler was lying to me)
The text was updated successfully, but these errors were encountered: