5. State as a library
Teaches: State is a handler, not a keyword
There is no mutable-variable keyword in bang. state 0 in … installs a
handler that interprets get/put as reads/writes against a seeded
value — mutability is ordinary library code over the same effect-and-handler
mechanism every other effect in this tour uses (kernel invariant: the only
privileged primitive is STM, and even that ships as a handler in v1).
state 0 in let c = {get}; z = put 5 in $cExpected output (bang run stdout):
5Run it yourself:
curl -fsSL https://raw.githubusercontent.com/phibkro/bang/main/tools/install.sh | sh
bang run examples/state/main.bang← 4. Pattern match: wildcards & mutual recursion · 6. Handling an effect →