7. Actors and fibers

Concurrency is actors on fibers: spawn makes an actor from a class with a receive method, send delivers one message at a time, and ownership MOVES with the message — no locks, no shared mutable state, no data races by construction. Blocking calls park the fiber; the shard serves others meanwhile. One VM per core by default; mailboxes are bounded (a full one is a catchable trap).

class Counter {
  total: Int
  fn receive(msg: Tick) {
    self.total = self.total + msg.n;
    print("total ${self.total}");
  }
}

class Tick {
  n: Int
}

fn main() -> Int {
  let c: actor Tick = spawn Counter { total: 0 };
  send(c, Tick { n: 1 });
  send(c, Tick { n: 2 });
  time.sleep(50);   -- parks this fiber; the actor runs
  return 0;
}

Chapters