Skip to content

Instantly share code, notes, and snippets.

@rossabaker
Created June 18, 2010 02:00
Show Gist options
  • Save rossabaker/443103 to your computer and use it in GitHub Desktop.
Save rossabaker/443103 to your computer and use it in GitHub Desktop.
Scala equality pitfall
scala> null == 42
res0: Boolean = false
scala> (null:java.lang.Integer) == 42
java.lang.NullPointerException
at scala.runtime.BoxesRunTime.equalsNumNum(Unknown Source)
at scala.runtime.BoxesRunTime.equalsNumObject(Unknown Source)
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun...
@rossabaker
Copy link
Author

The second expression also returns false in Scala 2.7.7.

@paulosuzart
Copy link

Thinks are becoming a bit sad. :(

@rossabaker
Copy link
Author

It does some magic to allow different numeric types to be equal if they have the same value (for example., 5 == 5.0d). A lot of complexity comes with that magic, I think.

@paulosuzart
Copy link

Arrays redesign with java interoperability and all that Manifest stuff is another point. Language gaining bit verbose with code not related to what you really want to do. Scala is loosing points and the chance to be the next big language. :(

@rossabaker
Copy link
Author

I posted the equality problem to the Scala mailing list, and within a couple hours, a fix was committed. Hooray for Paul Phillips and the rest of the Scala dev team.

http://scala-programming-language.1934581.n4.nabble.com/and-equals-td2261488.html#a2262102
http://lampsvn.epfl.ch/trac/scala/changeset/22379/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment