问题
I have been scratching my head to understand how to read this function:
private def greeterBehavior(currentGreeting: String): Behavior[Command] =
Actor.immutable[Command] { (ctx, msg) =>
msg match {
case WhoToGreet(who) =>
greeterBehavior(s"hello, $who")
case Greet =>
println(currentGreeting)
Actor.same
}
}
Questions:
1 ) function takes string and returns Behavior[Command]
. ( understood)
But .. what is Actor.immutable[Command]
?
Is this a type casting ? or is it an object ?
2) if I have to understand such syntax what is the best place or book I can refer?
回答1:
immutable
is a method on Actor
, which takes in a generic type parameter, in this case that type is Command
.
Any intro to Scala material worth reading should cover generics. "Programming in Scala" and "Scala for the Impatient" are both popular.
回答2:
To address the comments regarding the location of the API documentation for Actor.immutable
:
As the Akka documentation clearly states, the Akka Typed API is still in flux:
This module is currently marked as may change in the sense of being the subject of active research. This means that API or semantics can change without warning or deprecation period and it is not recommended to use this module in production just yet—you have been warned.
Apparently you're using a pre-2.5.10 version of Akka: the Actor
object was removed from the Akka Typed module in version 2.5.10.
- From Akka 2.5.2 to Akka 2.5.8, there was an
akka.typed.scaladsl.Actor.immutable
method. - In Akka 2.5.9, the Actor.immutable method moved to the
akka.actor.typed
package. - In Akka 2.5.10, the
akka.actor.typed.Actor
object was removed. This object is still absent in 2.5.11 and 2.5.12 (the current version of Akka at the time of this writing).
Here is the Scaladoc for the last version of Actor.immutable
from Akka 2.5.9:
def immutable[T](onMessage: (ActorContext[T], T) => Behavior[T]): Immutable[T]
Construct an actor behavior that can react to both incoming messages and lifecycle signals. After spawning this actor from another actor (or as the guardian of an
akka.actor.typed.ActorSystem
) it will be executed within anActorContext
that allows access to the system, spawning and watching other actors, etc.This constructor is called immutable because the behavior instance does not need and in fact should not use (close over) mutable variables, but instead return a potentially different behavior encapsulating any state changes.
来源:https://stackoverflow.com/questions/49901362/scala-syntactic-sugar