Are multiple self-types possible?
Solution 1:
You can have a single self-type which is a compound type.
Try this:
trait A {
def aValue = 1
}
trait B {
def bValue = 1
}
trait C {
self: A with B =>
def total = aValue + bValue
}
class ABC extends A with B with C
Solution 2:
With one trait you can do it with structural type:
trait C {
self: { def aValue: Int
def bValue: Int } =>
def total = aValue + bValue
}
class ABC extends C {
def aValue = 1
def bValue = 1
}
Reflection used.
But, firstly you should not overuse self-types because of principle of least power.
Methods from question can be added simply by extending other tait:
trait C extends A with B{
def total = aValue + bValue
}
or type both methods explicitly:
trait C {
def aValue: Int
def bValue: Int
def total = aValue + bValue
}
Where to use self-types?
Self-types are usually used with classes. This is a good way for trait to require being a subclass of a desired class.
There is also one good use of self-type with triats: when you want to manipulate order of class initialization with multi-inheritance.