Skip to main content
Version: ZIO 2.x

Introduction

ZIO contains a few data types that can help you solve complex problems in asynchronous and concurrent programming. ZIO data types categorize into these sections:

  1. Core Data Types
  2. Contextual Data Types
  3. Concurrency
  4. Resource Safety
  5. Streaming
  6. Miscellaneous

Core Data Types#

  • ZIO โ€” A ZIO is a value that models an effectful program, which might fail or succeed.
    • UIO โ€” An UIO[A] is a type alias for ZIO[Any, Nothing, A].
    • URIO โ€” An URIO[R, A] is a type alias for ZIO[R, Nothing, A].
    • Task โ€” A Task[A] is a type alias for ZIO[Any, Throwable, A].
    • RIO โ€” A RIO[R, A] is a type alias for ZIO[R, Throwable, A].
    • IO โ€” An IO[E, A] is a type alias for ZIO[Any, E, A].
  • Exit โ€” An Exit[E, A] describes the result of executing an IO value.
  • Cause - Cause[E] is a description of a full story of a fiber failure.
  • Runtime โ€” A Runtime[R] is capable of executing tasks within an environment R.

Contextual Data Types#

  • Has โ€” The trait Has[A] is used with the ZIO environment to express an effect's dependency on a service of type A.
  • ZLayer โ€” The ZIO[-R, +E, +A] data type describes an effect that requires an input type of R, as an environment, may fail with an error of type E or succeed and produces a value of type A.
    • RLayer โ€” RLayer[-RIn, +ROut] is a type alias for ZLayer[RIn, Throwable, ROut], which represents a layer that requires RIn as its input, it may fail with Throwable value, or returns ROut as its output.
    • ULayer โ€” ULayer[+ROut] is a type alias for ZLayer[Any, Nothing, ROut], which represents a layer that doesn't require any services as its input, it can't fail, and returns ROut as its output.
    • Layer โ€” Layer[+E, +ROut] is a type alias for ZLayer[Any, E, ROut], which represents a layer that doesn't require any services, it may fail with an error type of E, and returns ROut as its output.
    • URLayer โ€” URLayer[-RIn, +ROut] is a type alias for ZLayer[RIn, Nothing, ROut], which represents a layer that requires RIn as its input, it can't fail, and returns ROut as its output.
    • TaskLayer โ€” TaskLayer[+ROut] is a type alias for ZLayer[Any, Throwable, ROut], which represents a layer that doesn't require any services as its input, it may fail with Throwable value, and returns ROut as its output.

Concurrency#

Fiber Primitives#

  • Fiber โ€” A fiber value models an IO value that has started running, and is the moral equivalent of a green thread.
  • FiberRef โ€” FiberRef[A] models a mutable reference to a value of type A. As opposed to Ref[A], a value is bound to an executing Fiber only. You can think of it as Java's ThreadLocal on steroids.
  • Fiber.Status โ€” Fiber.Status describe the current status of a Fiber.
  • FiberId โ€” FiberId describe the unique identity of a Fiber.

Concurrency Primitives#

  • ZRef โ€” A ZRef[EA, EB, A, B] is a polymorphic, purely functional description of a mutable reference. The fundamental operations of a ZRef are set and get.
    • Ref โ€” Ref[A] models a mutable reference to a value of type A. The two basic operations are set, which fills the Ref with a new value, and get, which retrieves its current content. All operations on a Ref are atomic and thread-safe, providing a reliable foundation for synchronizing concurrent programs.
  • ZRef.Synchronized โ€” A ZRef.Synchronized[RA, RB, EA, EB, A, B] is a polymorphic, purely functional description of a mutable reference.
    • Ref.Synchronized โ€” Ref.Synchronized[A] models a mutable reference to a value of type A in which we can store immutable data, and update it atomically and effectfully.
  • Promise โ€” A Promise is a model of a variable that may be set a single time, and awaited on by many fibers.
  • Queue โ€” A Queue is an asynchronous queue that never blocks, which is safe for multiple concurrent producers and consumers.
  • Hub - A Hub is an asynchronous message hub that allows publishers to efficiently broadcast values to many subscribers.
  • Semaphore โ€” A Semaphore is an asynchronous (non-blocking) semaphore that plays well with ZIO's interruption.

STM#

  • STM - An STM represents an effect that can be performed transactionally resulting in a failure or success.
  • TArray - A TArray[A] is an array of mutable references that can participate in transactions.
  • TSet - A TSet is a mutable set that can participate in transactions.
  • TMap - A TMap[A] is a mutable map that can participate in transactions.
  • TRef - A TRef is a mutable reference to an immutable value that can participate in transactions.
  • TPriorityQueue - A TPriorityQueue[A] is a mutable priority queue that can participate in transactions.
  • TPromise - A TPromise is a mutable reference that can be set exactly once and can participate in transactions.
  • TQueue - A TQueue is a mutable queue that can participate in transactions.
  • TReentrantLock - A TReentrantLock is a reentrant read / write lock that can be composed.
  • TSemaphore - A TSemaphore is a semaphore that can participate in transactions.

Resource Safety#

  • ZManaged โ€” A ZManaged is a value that describes a perishable resource that may be consumed only once inside a given scope.
    • Managed โ€” Managed[E, A] is a type alias for ZManaged[Any, E, A].
    • TaskManaged โ€” TaskManaged[A] is a type alias for ZManaged[Any, Throwable, A].
    • RManaged โ€” RManaged[R, A] is a type alias for ZManaged[R, Throwable, A].
    • UManaged โ€” UManaged[A] is a type alias for ZManaged[Any, Nothing, A].
    • URManaged โ€” URManaged[R, A] is a type alias for ZManaged[R, Nothing, A].

Streaming#

  • ZStream โ€” A ZStream is a lazy, concurrent, asynchronous source of values.
    • Stream โ€” Stream[E, A] is a type alias for ZStream[Any, E, A], which represents a ZIO stream that does not require any services, and may fail with an E, or produce elements with an A.
  • ZTransducer โ€” A ZTransducer[R, E, I, O] is a stream transformer. Transducers accept a stream as input, and return the transformed stream as output.
  • ZSink โ€” A ZSink is a consumer of values from a ZStream, which may produces a value when it has consumed enough.
    • Sink โ€” Sink[E, A, L, B] is a type alias for ZSink[Any, E, A, L, B].
  • SubscriptionRef โ€” A SubscriptionRef[A] contains a current value of type A and a stream that can be consumed to observe all changes to that value.

Miscellaneous#

  • Chunk โ€” ZIO Chunk: Fast, Pure Alternative to Arrays
  • Schedule โ€” A Schedule is a model of a recurring schedule, which can be used for repeating successful IO values, or retrying failed IO values.
  • Supervisor โ€” A Supervisor[A] is allowed to supervise the launching and termination of fibers, producing some visible value of type A from the supervision.

To learn more about these data types, please explore the pages above, or check out the Scaladoc documentation.