I have a situation where I have several traits with each trait containing a set of methods. I have now to expose these functionalities to the external world through profiles. For example., a basic profile will be another trait, but this contains just the methods a and 2 from trait 1, methods 4 and 5 from trait 2 etc.,I can create these profiles as a new trait and repeat the method implementation, but how can I reuse the already defined methods in my trait and just create the profile traits more elegantly? For example.,
trait InternalTrait1 {
def method1(int: Int): Int
def method2(int: Int): Int
}
trait InternalTrait2 {
def method3(int: Int): Int
def method4(int: Int): String
}
I would now like to have:
trait BasicProfile {
this: InternalTrait1 with InternalTrait2 =>
def method2(int: Int): Int = super.method2(int)
def method4(int: Int): String = super.method4(int)
}
I want to refer the method2 and method4 from Trait1 and Trait2 respectively. Any ideas? Self traits come to my mind, but not sure if that is a good idea?
Could you introduce a couple of traits more (like
Method2DoerandMethod4Doerbelow)?If none of the traits are in IS-A relation and you'd prefer not to introduce new traits then you can try to replace inheritance with composition (HAS-A relation)
or
In principle you can use structural types