Access overridden methods from mixin to Scala

I think I read somewhere that this is possible.

Use case

I want to create an attribute that when mixing in memoizes hashCode overwrites the method and stores the result of the overwritten method in val.

trait MemoHashCode { val hashCode = callToOverwritten_hashCode } 
+4
source share
1 answer

Just use the super keyword:

 trait MemoHashCode { val hashCode = super.hashCode } 

This is possible because each attribute implicitly extends AnyRef , which has hashCode . If you want to use methods that are not specific to each object, you will need to make sure that this attribute can only be mixed with objects that have a method that you intend to use. This is possible using self annotations:

 trait MemoSomethingElse { this: SomeType => // SomeType has method somethingElse val somethingElse = super.somethingElse } 
+9
source

All Articles