25 · State Machines
State-indexed machines
new_static creates an owned machine with a state witness. send consumes it and returns the destination state type; a guarded failure returns the original machine and event. Functions can require Order<.Paid>. erase_state returns a dynamic machine when static checking is no longer needed.
On this page
new_static creates an owned machine with a state witness. send consumes it and returns the destination state type; a guarded failure returns the original machine and event. Functions can require Order<.Paid>. erase_state returns a dynamic machine when static checking is no longer needed.
Complete example
State-indexed machines.
state_indexed_machines.zolo
machine Order {
state Draft, Paid, Shipped
event Pay, Ship
initial Draft
Draft -> Paid on Pay
Paid -> Shipped on Ship
}
fn ship(sink order: Order<.Paid>) -> Order<.Shipped> =
order.send(Order.Event.Ship)
let paid = Order.new_static().send(Order.Event.Pay)
let shipped = ship(sink paid)
let ordinary = shipped.erase_state()
match ordinary.state {
.Shipped => print("shipped"),
_ => assert(false)
}
// A guarded transition returns the original machine and event on failure.
machine Door {
state Closed, Open
event Unlock
initial Closed
Closed -> Open on Unlock if false
}
match Door.new_static().send(Door.Event.Unlock) {
.Ok(open) => { let ordinary = open.erase_state(); assert(false) },
.Err({ machine: closed, event: retry }) => {
let ordinary = closed.erase_state()
match retry { .Unlock => print("closed") }
}
}
Rules and behavior
Typed states and events · Documentation
From a checkout of the repository:
zolo examples/features/state_indexed_machines.zolo
See also