Let's say I have a data class that has three properties:
data class Product(
val id: Int,
val name: String,
val manufacturer: String)
If I understand correctly, Kotlin will generate equals()
and hashCode()
using all the three properties, which will be like:
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val that = other as Product?
return id == that.id &&
name == that!!.name &&
manufacturer == that.manufacturer
}
override fun hashCode(): Int {
return Objects.hash(id, name, manufacturer)
}
So what if I don't want id
to be used in equals()
and hashCode()
? Is there a way to tell Kotlin to ignore certain properties when generating these functions? How about toString()
and compareTo()
?