Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force trait to implement method

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.

like image 882
tobi Avatar asked Oct 22 '13 09:10

tobi


Video Answer


1 Answers

I thought about structural subtyping:

trait I[T<: { def:foo:Unit}]

....

would that work for you?

like image 98
Stefan Kunze Avatar answered Nov 13 '22 18:11

Stefan Kunze