Share in common between traits

I have two features

trait Base[A] trait SometimesUsedWithBase[A] { this: Base[A] => } 

Then i use them with class

 class StringThing extends Base[String] with SometimesUsedWithBase[String] 

It would be great if I didn’t have to define the SometimesUsedWithBase type, and instead, it somehow understands that it uses the type defined in Base to make it look

 class StringThing extends Base[String] with SometimesUsedWithBase 

Is it possible?

+5
source share
1 answer

You should do something like this.

 trait Base[A] { type BaseType = A } trait SometimesUsedWithBase { this: Base[_] => def someFunction: BaseType } class StringThing extends Base[String] with SometimesUsedWithBase { def someFunction: String = "" } 
+7
source

All Articles