Override trait function in the intersection of two mixins

Viewed 60

The thing is, is there any way (That does not involve reflective black magic), to implicitly override a method, when two known traits are implemented?

Think that we have a class SysImpl that implements two mixins: System and Container:

// Where system acts as the interface for another consumer
trait System {
  def isEmpty = false
}

trait Container[A] {
  val elements = mutable.Set[A]()
}

// Then, we typically implement:
class SysImpl extends System with Container[Int] {
  // We override the default system implementation here
  override def isEmpty = elements.isEmpty
}

Just as an example. Is there any way of implementing either a third trait, or making something to the original, that makes the implementation implicitly override the isEmpty method, in case that System & Container[A] are present?

The first thing that comes to my mind is creating an extension method, but that would be shadowing at its best (Wouldn't it?). I need the method to be overridden properly, as the call is dispatched by a consumer who only sees Systems.

(Example, omitting details)

class AImpl extends System with Container[A]

object Consumer {
  def consume(sys: System) = if (sys.isEmpty) { /* Do things */ }
}

// Somewhere...
object Main {
  def main() = {
    Consumer.consume(new AImpl)
  }
}
1 Answers

Just

  trait Mix[A] extends Container[A] with System {
    override def isEmpty = elements.isEmpty
  }
Related