Typed actor context capture

I wonder is this a correct way to use context in typed actor:

  def behaviour: Behavior[Protocol] = Behaviors.setup[Protocol] { ctx ⇒
    // ....
        def initState(state: State): Behavior[Protocol] = Behaviors.receiveMessage {
          case Initialized =>
            // context from Behavior.setup
            ctx.log.debug(s"MESSAGE: Initialized")

          // ...
        }
        initState(State.empty)
  }

Do I need to use Behaviours.receive to access context or it is ok to use context from upper Behaviours.setup?

Absolutely :slight_smile:
This is how you’d use the functional style APIs.

1 Like

In that specific example, not using the context in the setup block, you could also use Behaviors.receive where you will get the context together with the message as parameters each time there is a message to process.