Good day, I'm trying to delegate `toString()`, `eq...
# getting-started
k
Good day, I'm trying to delegate
toString()
,
equals()
and
hashCode()
implementations to another object, so I'm delegating
Any
like below, is this the right way to do?
Copy code
interface A
class B: A {
  override fun toString() = "This is C..."
  override fun equals(...) = ...
  override fun hashCode() = ...
}
class C(delegated: B): A by delegated, Any by delegated {
  // toString(), equals() and hashCode() get `delegated to delegated`?
}
Edit: I'm getting the error:
[DELEGATION_NOT_TO_INTERFACE] Only interfaces can be delegated to
Since Any is not an interface, my current only solution is:
Copy code
class C(delegated: B): A by delegated {
    override fun toString(): String = delegated.toString()
    override fun equals(other: Any?): Boolean = delegated == other
    override fun hashCode(): Int = delegated.hashCode()
}
c
Your second message is correct, it's not possible to automatically delegate toString/equals/hashCode
thank you color 1