FuturesTopThe actor traits Reactor, ReplyReactor, and
ActorControl structures

Contents

Index

Control structures

The Reactor trait defines control structures that simplify programming with the non-returning react operation. Normally, an invocation of react does not return. If the actor should execute code subsequently, then one can either pass the actor's continuation code explicitly to react, or one can use one of the following control structures which hide these continuations.

The most basic control structure is andThen. It allows registering a closure that is executed once the actor has finished executing everything else.

actor {
  {
    react {
      case "hello" => // processing "hello"
    }: Unit
  } andThen {
    println("hi there")
  }
}

For example, the above actor prints a greeting after it has processed the "hello" message. Even though the invocation of react does not return, we can use andThen to register the code which prints the greeting as the actor's continuation.

Note that there is a type ascription that follows the react invocation (: Unit). Basically, it let's you treat the result of react as having type Unit, which is legal, since the result of an expression can always be dropped. This is necessary to do here, since andThen cannot be a member of type Nothing which is the result type of react. Treating the result type of react as Unit allows the application of an implicit conversion which makes the andThen member available.

The API provides a few more control structures:

The control structures can be used anywhere in the body of a Reactors act method and in the bodies of methods (transitively) called by act. For actors created using the actor { ... } shorthand the control structures can be imported from the Actor object.


November 30, 2010

FuturesTopThe actor traits Reactor, ReplyReactor, and
ActorControl structures

Contents

Index