A process keeps its own weather

A process is a small place where something can happen without the whole program happening to it at once.

Give the state an address

The BEAM gives these places identities and mailboxes. Pooka gives one useful arrangement of them a language construct: the actor.

actor Counter {
  var count: Int = 0

  be add(amount: Int) {
    count += amount
  }

  value() -> Int {
    count
  }
}

main() {
  let counter = Counter()
  counter.add(2)
  counter.add(3)
  assert counter.value() == 5
}

A message and a question

For this example’s single sender, the exchange looks like this:

caller -> add(2)  -> counter
caller -> add(3)  -> counter
caller -> value() -> counter
caller <- 5      <- counter

be add is a behaviour: calling it sends a message and returns without waiting for the state change. value() is a request that waits for an answer. The state belongs to the actor, and its handlers work through messages one at a time.

The example uses one sender. The two additions and the following request travel from that sender to the same actor in order. This is not a promise that messages from every process in the system share one universal arrival order.

That distinction matters. Concurrency explanations become rather too comfortable when they leave out the competing callers.

Decide who notices a failure

An actor also is not automatically a complete fault-tolerance policy. The BEAM has links, monitors and supervision machinery, but an application still needs to decide who should notice a failure and what should happen next. A mailbox alone cannot make that decision kindly on our behalf.

Erlang’s process documentation is worth reading alongside the language surface. The runtime beneath the actor has its own precise rules; a friendly keyword does not replace them.

What the boundary does give us is a place to put mutable state without making it everyone’s shared furniture. Ordinary class values remain immutable. The counter changes in the process that owns it.

There can be several kinds of weather in one running program. Each room should know which window is its own.

— Peregrine Finch

· pooka, actors, beam