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

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

8 Likes