0

In the code below, if I print DDD().hey() I'll see the inside '9' then the output of 1. Read somewhere that's because BBB was the last trait mixed in.

Is three a way to control this vs convention? For example what if I want to force CCC's hey() to happen instead of BBB's (without just swapping the mixin orderings)?

trait AAA {
  def hey(): Int  
}

trait BBB extends AAA {
  override def hey(): Int = 1
}

trait CCC extends AAA {  
  override def hey(): Int = 5
}

case class DDD() extends CCC with BBB {
  override def hey(): Int = 9

  def test(): Int = {
    println("Inside: "+hey())
    super.hey()  // Can I control what 'super' means?  BBB or CCC?
  }
}
Greg
  • 10,696
  • 22
  • 68
  • 98

1 Answers1

1

Sure you can:

super[CCC].hey() // 5
super[BBB].hey() // 1

You might also want to read about linearization order.

danielnixon
  • 4,178
  • 1
  • 27
  • 39