Ignoring certain properties when generating equals(), hashCode(), etc


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()?


No. If you need non-standard generation of any data class methods, you can override them and provide any implementation you need.


From the documentation:

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

For more information take a look at: https://kotlinlang.org/docs/reference/data-classes.html#properties-declared-in-the-class-body