ZState

sealed trait ZState[S]

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
class Object
trait Matchable
class Any

Value members

Abstract methods

def get(implicit trace: Trace): UIO[S]

Gets the current state.

Gets the current state.

def set(s: S)(implicit trace: Trace): UIO[Unit]

Sets the state to the specified value.

Sets the state to the specified value.

def update(f: S => S)(implicit trace: Trace): UIO[Unit]

Updates the state with the specified function.

Updates the state with the specified function.