I have a trait I (intermediary), a class M (mixer) mixing in the trait and a trait S (specific).
class M extends Something with S {
def baz() = foo()
}
trait I {
def foo(): { ...; bar(); ... }
def bar()
}
trait S extends I {
def bar() = 42
}
I serves as an intermediate layer between M and S, providing a common interface.
I have an implemented method foo in I that calls a method bar (not implemented in I but defined there). What I would like to achieve is that all traits extending I must implement bar, so that this would throw a compile time error because bar is not implemented:
trait Z extends I
Is this possible in Scala?
P.S.: I am aware of the answer of Force Scala trait to implement a certain method but I do not want that kind of explicit coupling.
I thought about structural subtyping:
trait I[T<: { def:foo:Unit}]
....
would that work for you?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With