Read this first, which talks about what the difference between using val
and def
inside a trait is in Scala. here.
You would use that pattern when your trait contains concrete implementation of some kind that relies on these implicits being there, but you want the final implementors of your trait to be the ones that specify concrete values for those implicits.
trait MyXXXProtocol extends Logging {
implicit def actor: ActorSystem
implicit def materializer: Materializer
implicit def executor: ExecutionContextExecutor
def test(input: String): Unit = {
// let's pretend this is valid
actor.props("bla").send(input)
}
This becomes useful if for instance the signature of send
is something like this:
def send(input: String)(
implicit system: ActorSystem,
materializer: FlowMaterializer
): Unit = ...
It's useful because you've now created a template but it's up to concrete implementors to supply the implicit system in which these things will execute, you are forwarding those implicits such that you can have some implementation defined where they are required, but you can supply a real implicit later.