ZState[S] models a value of type S that can be read from and written to
during the execution of an effect. The idiomatic way to work with ZState is
as part of the environment using operators defined on ZIO. For example:
final case class MyState(counter: Int)
for {
_ <- ZIO.updateState[MyState](state => state.copy(counter = state.counter + 1))
count <- ZIO.getStateWith[MyState](_.counter)
} yield count
Because ZState is typically used as part of the environment, it is
recommended to define your own state type S such as MyState above rather
than using a type such as Int to avoid the risk of ambiguity.
To run a stateful workflow, use the ZIO.stateful operator to allocate the
initial state.
- Companion:
- object