liveBook · Manning

Apply method

As explained in Akka in Action, Second Edition

An apply method is a specially named Scala method that can be invoked using the enclosing object's name as call syntax.

An apply method is a specially named Scala method that lets an object or class-like value be called using its enclosing name. Thus, ActorSystem(...) means ActorSystem.apply(...), and Wallet() means Wallet.apply(). section 2.2.2

Constructing an actor system

In ActorSystem(Wallet(), "wallet"), Wallet() invokes Wallet.apply(), while ActorSystem(...) invokes ActorSystem.apply(...). The resulting expression creates an actor system whose guardian behavior is Wallet(). section 2.2.2

Apply methods also make case-class instances usable where a function is expected. Worker.Parse(replyTo) is backed by a generated Worker.Parse.apply(replyTo); passing Worker.Parse lets the compiler supply that function in the ask request constructor. section 3.2.7

def apply[T](guardianBehavior: Behavior[T], name: String): ActorSystem[T]

The ActorSystem.apply signature shows the behavior and name parameters behind the concise ActorSystem(...) call. section 2.2.2

def apply(replyTo: ActorRef[Worker.Response]): Worker.Parse = 
new Worker.Parse(replyTo)

The generated Worker.Parse.apply method makes the case-class constructor available as the function required by ask. section 3.2.7

How Akka in Action, Second Edition covers it

How deep it goes
Explains call desugaring, generated case-class methods, and function expected types without descending into compiler implementation.
Examples
Uses Wallet(), ActorSystem(Wallet(), "wallet"), Worker.Parse(replyTo), and generated apply signatures.
What you can do after
Understand how Scala rewrites object and case-class call syntax into apply-method calls and function values.
How it connects
Connects Scala objects, case classes, functions, constructors, ActorSystem creation, and typed actor ask requests.

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.