Scala, Java and Equality
val filesHere = (new java.io.File(".")).listFiles val filesHere2 = (new java.io.File(".")).listFiles scala> filesHere == filesHere2
res0: Boolean = false
This is quite the opposite of intuition. I would prefer the Here and filesHere2 files to be equal.
This, of course, is due to the mismatch of semantics between Java and Scala, for example, about the equality of arrays or (files). Clearly, I missed something!
You can read here:
but it seems that if you did this: filesHere.sameElements(filesHere2) , that should be true.
The javadoc for this is here: http://www.scala-lang.org/api/2.6.0/scala/IterableProxy.html#sameElements%28Iterable%5BB%5D%29
UPDATE:
A few fragments of the first link that may be useful:
In Java, C ++, and C #, operational tests are for reference, not equality of values. In contrast, Rubys operator tests == for equality of values. Whatever language you use, be sure to remember that in Scala, == checks the value of equality.
Regarding == does not work as expected in the lists:
Although this may seem like inconsistency, encouraging an explicit test for the equality of two mutable data structures is a conservative approach by language developers. Ultimately, this should save you from unexpected results in your conventions.
UPDATE 2:
Based on Raphael's comments, I looked at the spec and it was dated two days ago for the most recent update, and I see them at http://www.scala-lang.org/files/archive/nightly/pdfs/ScalaReference.pdf :
Method equals: (Any)Boolean is structural equality, where two instances are equal if they both belong to the case class in question and they have equal (with respect to equals) constructor arguments. class AnyRef extends Any { def equals(that: Any): Boolean = this eq that final def eq(that: AnyRef): Boolean = . . . // reference equality So it looks like the definition has not changed in Scala 2.10.2, since the specification seems consistent. If the behavior is different, then if you write unit test, it should be sent as an error for Scala.
If I ruled the world, I would discount the Scala eq method on the grounds that the name is extremely mixed with equal and ==. Instead, in English there is a word that expresses personality, not equality: I would just call it .
Similarly, I would replace Scala ne (this is a terrible name, as it is an abbreviation and not clear) with isnt .
It seems to me that they really can be added to AnyRef, and the old methods are deprecated, even at this late stage.
The equals() method of Java arrays uses referential equality, not something more complicated, and Scala == is just Java equals() .
Comparison does not work properly because this Java API returns an array.
Scala arrays and Java arrays are the same behind the scenes, and although the Scala array looks like a class, it's just java.io.File [] (in this example).
That is why the equality check cannot be overridden. Scala should use Java semantics for it.
Consider the following example:
val filesHere = (new java.io.File(".")).listFiles.toList val filesHere2 = (new java.io.File(".")).listFiles.toList This will work as expected.