How does Kotlin implement equals and hashCode?

Ah yes that makes a lot of sense. Does this means though that in this example:

that the equals method is going to box value into a java.lang.Double or is it going to use the Double.compare(d1, d2) or equivalent method like that? In other words, does Kotlin’s auto-generated implementation of equals when you use a Double field box the double for the equality check or not? I’m guessing (and hoping) that it doesn’t

I’ve just checked it, and turns out the new shiny JVM-IR backend does box them before comparison, whereas the old JVM backend was smart enough to call Double.compare. That’s a shame, should be reported to YT.

UPD: I’ve filed https://youtrack.jetbrains.com/issue/KT-48635

2 Likes

Got bitten by this again recently.

When you have arrays in a data class you get a warning that you should probably customise equals and hashCode… I’m starting to think I should also get the same warning for Double.

1 Like