Search code examples
scalaakkaakka-typed

Scala : Syntactic Sugar


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?


Solution

  • 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.