I suggest that `object` types automatically satisf...
# language-proposals
m
I suggest that
object
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 🤔
Copy code
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
.
g
Would be great if not everything would inherit from
Object
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 JVM
m
Yeah and you’d probably need support for
Self
types.
l
Well, the default
toString
for
object
is already correct…
m
What is a correct implementation of
hashCode
for an
object
?
m
The default one of
java.lang.Object
is sufficient. Or literally any static one 🙂