Marc Knaup
01/01/2021, 8:10 PMobject
types automatically satisfy equals
and hashCode
requirements of an interface/superclass.
object
types are typically equal to themselves and have a hash code based on their instance automatically which is sufficient.
I can only think of very very few exceptions where you would want an object to not be equal to itself or have some custom implementation 🤔
interface Foo {
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
}
data class Bar1(val bar: Int) : Foo // fine
object Bar2 : Foo // error because equals() and hashCode() are missing
I’m often using explicit abstract equals
and hashCode
to not forget to provide an implementation in implementing types. Typically I use data class
or object
but it’s too easy to forget the data
.georg
01/01/2021, 9:01 PMObject
implicitly anyway. Like in Swift where you need to explicitly conform to the Equatable
and Hashable
protocols. But I guess that’s inevitable if you live on the JVMMarc Knaup
01/01/2021, 9:02 PMSelf
types.louiscad
01/02/2021, 12:32 PMtoString
for object
is already correct…MiSikora
01/02/2021, 11:15 PMhashCode
for an object
?Marc Knaup
01/03/2021, 3:20 AMjava.lang.Object
is sufficient.
Or literally any static one 🙂