0

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
  • The prose-documentation suggests that this should be akka.typed.scaladsl.Actor-object. However: 1) I couldn't find any scaladocs online. 2) There isn't even Actor.scala in the package akka-actor-typed/src/main/scala/akka/actor/typed/scaladsl on github. I personally find the state of documentation (or rather, lack thereof), and the organization of the package a bit astonishing. Commented Apr 18, 2018 at 14:35

2 Answers 2

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 an ActorContext 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

How great that there is just one single Actor object in the entire universe, and that there are no problems at all finding akka.typed.scaladsl.Actor in the scaladocs...
@AndreyTyukin yeah, I don't know akka at all, but it still took way too long for me to find the correct Actor, and I didn't even see it in the scaladoc. It could definitely be improved
@One Looks like valid scala to me, although the awkward formatting makes it a little hard to tell (looks like you included the repl responses as well, instead of just code). I'm not sure how it relates to your question or my answer though. Could you clarify what you are looking to understand?
Correction : Is this a correct example in plain scala ? def abc(x : Int => Int) : Int = { x(10)} def one(a :Int) = abc { (x:Int) => x+a } var f1 = one(10)
@ Andrey Tyukin . Could you please help me find API documentation for this ? I could not find scala type help for given package/object.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.