Billy Newman
06/22/2023, 2:54 PMBilly Newman
06/22/2023, 2:58 PMopen class Foo(
var foo: String? = null
)
data class Bar(
val bar: String,
): Bookmark() {
// implement my hashCode and equals to account for bar and foo fields.
}
Since a data class does not include inherited fields in hashCode and equals I will need to provide my own implementation. I did try to implement hashCode and equals in Foo, but that doesn’t help either with inheritance. If there are a lot of fields in Bar, this leads to a lot of “boilerplate” code. Is there a way to take advantage of Bars already implemented hashCode and equals methods?
Possibly this for equals using ‘===’?
override fun equals(other: Any?): Boolean {
return (other is Bar) &&
this === other &&
foo == other.foo
}
Joffrey
06/22/2023, 3:46 PMkotlin
data class Bar(
val bar: String,
override val foo: String? = null,
) : Foo()
Paul Griffith
06/22/2023, 3:47 PMdata class
at all, reallyBilly Newman
06/22/2023, 4:06 PMKlitos Kyriacou
06/22/2023, 4:10 PMdata class Bar(val bar: String, val foo: Foo): Foo by foo
Joffrey
06/22/2023, 4:11 PMJoffrey
06/22/2023, 4:19 PMFoo
into an interface first