Effective Kotlin: Item 10 — Obey the general contract when overriding equals

With item 10 of Effective Java, Joshua Bloch goes into great detail of how to write an excellent equals method and doing the same in Kotlin is no exception.

The data class is one of Kotlin’s most touted features as they already implement equals for you. However, one thing to be aware of is that as of Kotlin 1.2 the order in which Kotlin compares properties of a data class follows the order of declaration. Therefore the code below will first compare the name and then the age: