Hi. How to implement properly `equals` and `hashco...
# announcements
i
Hi. How to implement properly
equals
and
hashcode
for a data class that contains a
ByteArray
?
s
IntelliJ will generate one for you if you tell it to through the intentions menu, but for the specialized arrays you should just be able to use
.contentEquals()
and
.contentHashCode()
for equals and hashcode, respectively
though I wonder, if the solution is that easy/literally able to be generated by the IDE, why isn’t that just part of the standard data class codegen rules? Maybe there are cases where you wouldn’t want that and we’re given the option to just suppress the inspection if that’s the case? Couldn’t begin to speculate what kind of scenarios that’d be useful in though
1
a
it depends, if you need reference equality, then it's one thing, if deep equality, then you have to do your own logic.
d
But you don't have to do your own logic for deep equality when you nest other data classes
a
Kotlin doesn't do deep equals for arrays
it uses reference equality
i
thanks! And what's an efficient way to do deep equals for byte arrays? Probably an index based for loop comparing the bytes?
d
Call
array.contentEquals(otherArray)
@iex
i
thanks @Dico