An implicit argument is a method parameter that Scala supplies automatically from a compatible implicit value in scope. In Akka’s ask pattern, this removes the need to pass the response timeout explicitly at every call site. section 3.2.4 section 3.2.7
Use the mechanism in three steps:
- Declare a compatible value, such as implicit val timeout: Timeout = Timeout(3, SECONDS). section 3.2.4
- Keep that value in a scope searched by the compiler, such as the same class. section 3.2.4
- Call context.ask(worker, Worker.Parse) without supplying responseTimeout; Scala inserts the compatible timeout. section 3.2.4 section 3.2.7
A manager asking a worker
A manager creates a worker and asks it to parse text. The ask operation waits up to three seconds, then maps a successful Worker.Done response to a success report or a failure to an error report. The timeout is supplied implicitly when declared in scope. Worker.Parse also receives a replyTo actor automatically, so the manager can construct it without explicitly providing that parameter. section 3.2.4 section 3.2.7
Listing 3.4 Manager that asks
object Manager {
sealed trait Command #1
case class Delegate(texts: List[String]) extends Command #1
private case class Report(description: String) extends Command #1
def apply(): Behavior[Command] =
Behaviors.setup { context =>
val timeout: Timeout = Timeout(3, SECONDS) #2
Behaviors.receiveMessage { message =>
message match {
case Delegate(texts) => #3
files.map { file =>
val worker: ActorRef[Worker.Command] = #4
context.spawn(Worker(file), s"worker-$file")
context.ask(worker, Worker.Parse)(timeout) { #5
case Success(Worker.Done) => #6
Report(
s"$text read by ${worker.path.name}") #6
case Failure(ex) =>
Report(
s"parsing '$file' has failed with [${ex.getMessage()}")
}
}
Behaviors.same
case Report(description) =>
context.log.info(description) #7
Behaviors.same
}}}
}
Inspect the manager implementation to see the explicit timeout variant alongside worker creation, asking, and success-or-failure response mapping. section 3.2.4

