ludic/examples/actions/reads.ludic
Orkuncakilkaya c64f8750e2 feat(lang): 0.R5 - a reducer writes one state and may read others, declared between its state and the action
What only becomes known inside the drain - a value another reducer just set, the map in play - no longer
has to be faked into the action, so a verb that reads several systems while it changes one is a reducer
instead of an act handed its states. A second state to write is still refused, and the message says the
way out. examples/actions/reads.ludic; reseeded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 18:23:48 +03:00

23 lines
827 B
Text

# reads.ludic - 0.R5: a reducer writes one state and may READ others, declared between its state and
# the action: what only becomes known inside the drain (a price another reducer set) need not ride in
# the action, and still no reducer changes a state it does not own.
program ReducerReads {
state Shop { price: int = 3 }
state Wallet { cash: int = 10 }
state Pack { n: int = 0 }
action Rise { by: int }
action Buy { }
reducer Shop on Rise(s: mut Shop, a: Rise) { s.price += a.by }
reducer Wallet on Buy(w: mut Wallet, s: Shop, a: Buy) {
if w.cash >= s.price { w.cash -= s.price }
}
reducer Pack on Buy(p: mut Pack, w: Wallet, a: Buy) { p.n += 1 }
entry (s: Shop, w: Wallet, p: Pack) {
dispatch Rise { by: 2 }
dispatch Buy { }
drain_actions()
print(`{s.price} {w.cash} {p.n}`)
}
}