I'm new to the Scala world and I'm using PLAY to make an API. It's going well but I am having some trouble understanding some of the notation and there is not a lot of documentation on it. Specifically, I am confused by the following controller method in one of the examples on the PLAY site:
class HomeController @Inject()(@Named("userParentActor") userParentActor: ActorRef,
cc: ControllerComponents)
(implicit ec: ExecutionContext) {
}
My question is what is going on in this constructor? Which part is the constructor and which part is the injected parameters? Is the ExecutionContext
injected as well? And why is the ec
in a separate parenthesis?
Thank you for the clarification.
It's just a constructor with two parameter lists, and the parameter in the second list is implicit.
class HomeController @Inject()( // list-1 start
@Named("userParentActor") userParentActor: ActorRef, // list-1, arg-1
cc: ControllerComponents // list-1, arg-2
)( // 1 end, 2 start
implicit ec: ExecutionContext // list-2, arg-1
) { // list-2 end
// body
}
The @Inject
annotation applies to both argument lists, so ec
is also injected by guice (using Play's default thread pool).
The @Named
annotation affects only the first argument.
The ec
argument is in a separate list, because implicit arguments must be declared in a separate list.
It is declared in a separate list probably because the author anticipated the use case where the controller is instantiated manually instead by the dependency injection container: it is simpler then, because you don't have to specify the default thread pool everywhere.