Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala, Java and equality

val filesHere = (new java.io.File(".")).listFiles
val filesHere2 = (new java.io.File(".")).listFiles

scala> filesHere == filesHere2
res0: Boolean = false

That is quite counter intuitive. I would rather expect that filesHere and filesHere2 are equal.

This is certainly due to a semantics mismatch between Java and Scala, e.g., about arrays or (files) equality. Clearly, I am missing something here!

like image 376
acherm Avatar asked Aug 06 '10 09:08

acherm


People also ask

What does == mean in Scala?

But in Scala, == is testing for value equality. Let's understand with example. Example : Scala.

What is the difference between .equals and == in Scala?

== is a final method, and calls . equals , which is not final. This is radically different than Java, where == is an operator rather than a method and strictly compares reference equality for objects.


1 Answers

If I ruled the world, I would deprecate Scala's eq method on the grounds that the name is extremely confusible with equals and ==. Instead English does have a word which expresses identity as opposed to equality: I would simply call it is .

Similarly I would replace Scala's ne (which is a terrible name, since it's both an abbreviation and incomprehensible) with isnt .

Seems to me these could actually be added to AnyRef and the old methods deprecated, even at this late stage.

like image 124
Jonathan Avatar answered Oct 16 '22 19:10

Jonathan