liveBook · Manning

Implicit argument

As explained in Akka in Action, Second Edition

An implicit argument is a Scala method argument that the compiler can automatically supply from a compatible implicit value.

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

How Akka in Action, Second Edition covers it

How deep it goes
Defines compiler lookup and demonstrates timeout insertion, but defers Scala’s broader implicit-scope rules.
Examples
Uses a manager parsing text through workers, with a three-second timeout and success-or-failure reports.
What it assumes
Assumes familiarity with Scala methods and Akka actors while teaching implicit values and automatic argument filling from the example.
What you can do after
Apply it via Akka’s ask API by declaring a compatible implicit timeout and omitting that parameter from calls.
How it connects
Connects Scala implicits with the ask pattern, response timeout, actor references, and the ActorSystem.

Also explained in

Compare how 2 books explain implicit argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Akka in Action, Second Edition. The section links point to the parts of the book it draws on.