Search code examples
scalamultiple-inheritanceinner-classes

Scala inner type under lattice inheritance


Consider the following code:

trait A {
  trait T
  def t: T
}
trait B1 extends A {
  trait T extends super.T
  def t: T
}
trait B2 extends A {
  trait T extends super.T
  def t: T
}
trait C extends B1 with B2 {
  trait T extends super.T // super.T means only B2.T, not B1.T
  //trait T extends B1.T with B2.T // Actually I want to do this
  def t: T
}

This will occur a compile error because type T in trait C doesn't inherit B1.T. But in trait C, I can't get B1.T by calling super.T. How can I resolve this problem?


Solution

  • In Scala you can refer to the specific superclass you want:

    trait C extends B1 with B2 {
      trait T extends super[B1].T with super[B2].T
      def t: T
    }