Skip to content

Instantly share code, notes, and snippets.

@ciuncan
Created February 18, 2015 16:53
Show Gist options
  • Save ciuncan/85c2d9623684cc68ddea to your computer and use it in GitHub Desktop.
Save ciuncan/85c2d9623684cc68ddea to your computer and use it in GitHub Desktop.
Diamond inheritance with abstract type member overriding
trait BaseImp
trait Base {
type T <: BaseImp
val TVal: T
}
trait Sub1Imp extends BaseImp {
def x = 1
}
trait Sub1 extends Base {
type T <: Sub1Imp
}
object Sub1 extends Sub1 {
type T = Sub1Imp
val TVal = new Sub1Imp {}
def x = "Sub1x - " + TVal.x
}
trait Sub2Imp extends BaseImp {
def x = 2
def y = 3
}
trait Sub2 extends Base {
type T <: Sub2Imp
def x = "Sub2x - " + TVal.x
def y = "Sub2y - " + TVal.y
}
object Sub2 extends Sub2 {
type T = Sub2Imp
val TVal = new Sub2Imp {}
}
trait DiamondSubImp extends Sub1Imp with Sub2Imp {
override def x = super.x
def z = 4
}
trait DiamondSub extends Sub1 with Sub2 {
type T <: DiamondSubImp
override def x = "DiamondSubx - " + TVal.x
def z = "DiamondSubz - " + TVal.z
}
object DiamondSub extends DiamondSub {
type T = DiamondSubImp
val TVal = new DiamondSubImp {}
}
println(s"Sub1.x = ${Sub1.x}")
println(s"Sub2.x = ${Sub2.x}")
println(s"Sub2.y = ${Sub2.y}")
println(s"DiamondSub.x = ${DiamondSub.x}")
println(s"DiamondSub.y = ${DiamondSub.y}")
println(s"DiamondSub.z = ${DiamondSub.z}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment