The question is how C trait uses str field specified in D class. D extends from A so it doesn't have str specified in any contract. Even though C is able to use it. When I remove str from D compiler won't compile this code. How does it know that str from D unrelated to str in B is the same thing. I would expect this behavior in dynamically typed language but not in one with static typing.
trait A {
def receive(): PartialFunction[String, Any]
}
trait B extends A {
def str: String
}
trait C extends B {
abstract override def receive(): PartialFunction[String, Any] =
cReceive.orElse(super.receive())
val cReceive: PartialFunction[String, Any] = {
case "MemberUp" ⇒ s"$str"
}
}
class D extends A {
override def receive(): PartialFunction[String, Any] = {
case _ ⇒
}
def str: String = "My string"
}
class E extends D with C
object Main extends App {
private val e = new E
println(e.receive().apply("MemberUp"))
}