Skip to content

25 · State Machines

Typed events and state payloads

A typed machine owns its State and Event enums. Transition patterns bind event and state payloads only within that transition. Initial payloads run once per instance. send returns false if no transition applies; can_send checks patterns without executing guards or actions.

On this page

A typed machine owns its State and Event enums. Transition patterns bind event and state payloads only within that transition. Initial payloads run once per instance. send returns false if no transition applies; can_send checks patterns without executing guards or actions.

Complete example

Typed events and state payloads.

06-typed-events.zolo
Playground
// Typed catalogs make states and events ordinary nominal enum values.

machine Loader {
    state Idle, Ready(value: int)
    event Loaded(value: int), Reset
    initial Idle

    Idle -> Ready(value) on Loaded(value) if value > 0
    Ready(previous) -> Idle on Reset
}

let loader = Loader.new()
print(loader.can_send(Loader::Event::Loaded(-1))) // true: structural applicability only

print(loader.send(Loader::Event::Loaded(-1)))     // false: the guard is checked by send

print(loader.send(Loader::Event::Loaded(7)))      // true

if let Loader::State::Ready(value) = loader.state { print(value) } // 7

print(loader.send(Loader::Event::Loaded(8)))      // false: no transition from Ready

print(loader.send(Loader::Event::Reset))         // true

print(match loader.state { Loader::State::Idle => true, Loader::State::Ready(_) => false })       // true

Rules and behavior

Typed states and events · Documentation

From a checkout of the repository:

zolo examples/features/26-state-machines/06-typed-events.zolo

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

enespt-br