become
Signature
Section titled “Signature”become : behaviour → { next-behaviour: behaviour }Description
Section titled “Description”become new-beh returns { next-behaviour = new-beh }. The actor uses the next-behaviour field to replace the current behaviour after processing each message.
Without become, every message is handled by the initial behaviour. With become, each message can leave a different behaviour for the next — this is how state machines work.
Example
Section titled “Example”counter = count: msg: if msg == "inc" then reply (count + 1) // become (counter (count + 1)) else if msg == "get" then reply count else { };After "inc", become (counter (count + 1)) installs a new behaviour that has count + 1 closed over. The next message will call counter (count + 1).
State machine
Section titled “State machine”toggle = state: msg: if msg == "flip" then reply.right (! state) // become (toggle (! state)) else if msg == "query" then reply.right state else { };
toggle-c = actor (toggle false);(toggle-c { inbox = st "flip" "query" "flip" "query"; }).outbox.toList# [ { right = true; } { right = true; } { right = false; } { right = false; } ]becomeis only meaningful inside a behaviour returned toactor. Using it in a plain function has no special effect — it just produces an attrset.- If
next-behaviouris absent, the current behaviour is reused for the next message. becomeis stateless itself — it just packages a function reference into an attrset field.