DEV Community

Discussion on: Kotlin - The Good, the Bad and the Ugly

Collapse
 
tbroyer profile image
Thomas Broyer

Wrt data classes, having inheritance would break the Liskov Substitution Principle wrt equals(), making things break as soon as you use such objects in sets or as map keys.

There's some discussion about this in errorprone.info/bugpattern/EqualsG...

If all you want is a DTO and don't really care about equals et al (or all the things you get for free with a data class, i.e. destructuring assignment and copy()), then just use a class, not a data class:

class Person(
  val firstName: String,
  val lastName: String
)

I'd say the biggest problem of data classes is that they're too often abused.

See also jakewharton.com/public-api-challen...