Equals overloading in Kotlin

Defining equals and hashCode is considered somewhat useless on object declarations that have no explicit supertype, according to this issue. Probably correct equals+hashCode implementations on objects have few use cases.

There's even an IDE inspection that shows a warning when you try to do so:

IDE warning screenshot
The warning is not there when the object has a declared supertype.

However, I don't think that some technical reason stops Kotlin from resolving the overloaded operator, and the whole behaviour is strange, so I filed an issue in Kotlin issue tracker.

As for now (Kotlin 1.0.2 EAP), even with declared supertype you can only check object's equality with instances of exactly the same declared type which it has as a supertype:

object SomeObject : List<Int> by listOf() { ... }
SomeObject == listOf(1, 2, 3) // OK
SomeObject == arrayListOf(1, 2, 3) // not resolved (why?)

object MyObject : Any() { ... }
MyObject == 1 // error
MyObject == 1 as Any // OK o_O

object AnotherObject { ... }
AnotherObject == 1 as Any // works! Probably Any is supertype here

As to defining equals as an extension function: no, you can't do it because extensions are resolved statically and are shadowed by members (there's a similar question about toString).

Tags:

Kotlin