2

I'm looking for the best syntax for having contained classes pick up the implicits exposed by their containing classes?

The scenario I have has two dependencies abstracted for testing: The creation of the application's actorsystem, and a webclient that happens to require the actorsystem as well:

trait Core {
  implicit def system: ActorSystem
}

trait WebClient {
  implicit def system: ActorSystem
}

trait Api extends WebClient {
  ...
}

trait Server extends Core {
  def api: Api
  val service = system.actorOf(Props(new Service(api)))
}

Now, i create a new App which provides the actorsystem and api, but the Api needs an implicit actorsystem, and the only way I've figured out is to manually provide it like this:

object ServerApp extends App with Core {

  implicit val system = ActorSystem("foo")

  val api = new Api {
    override implicit def system = implicitly[ActorSystem]
  }

}

Is there a better syntax for having WebClient pick up the implicit from Core? I can't have it extend Core, since it is contained by the Core implementation and required as a dependency for something else that is contained there. But override implict def system = implicitly[ActorSystem] seems rather hamfisted for something that should be, er, implicit

Arne Claassen
  • 14,088
  • 5
  • 67
  • 106

1 Answers1

2

You're shadowing the implicit value since they have the same name.

You might consider

class Api()(implicit val system: ActorSystem)

and

val api = new Api
som-snytt
  • 39,429
  • 2
  • 47
  • 129