I think I've read somewhere that this is possible.
I want to create a trait that when mixed in memoizes the hashCode by overwriting the method and storing the result of the overwritten method in a val.
trait MemoHashCode {
val hashCode = callToOverwritten_hashCode
}
Simply use the super
keyword:
trait MemoHashCode {
val hashCode = super.hashCode
}
That is possible because every trait implicitly extends AnyRef
which has hashCode
defined.
If you want to use methods not defined on every object you would have to make sure that the trait can only be mixed in with objects that have the method implemented which you are going to use. That is possible via a self type annotation:
trait MemoSomethingElse {
this: SomeType => // SomeType has method somethingElse
val somethingElse = super.somethingElse
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With