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 && // Possible to not use "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()?
Note that the compiler only uses the properties defined inside the primary constructor for the automatically generated functions. To exclude a property from the generated implementations, declare it inside the class body.
Your example has to look like this:
data class Product(
val name: String,
val manufacturer: String) {
val id: Int
}
data class Product private constructor(
val name: String,
val manufacturer: String) {
private var _id: Int = 0
val id get() = _id
constructor(name: String, manufacturer: String, id: Int): this(name, manufacturer) {
_id = id
}
}
fun main() {
val product1 = Product("hello", "world", 1)
val product2 = Product("hello", "world", 2)
println(product1 == product2)
}