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:
- Core Data Types
- Control Flow
- Scheduling
- Contextual Data Types
- State Management
- Concurrency
- Resource Management
- Streaming
- Logging
- Metrics
- Testing
- Miscellaneous
Core Data Types​
- ZIO —
ZIOis a value that models an effectful program, which might fail or succeed. - ZIOApp —
ZIOAppand theZIOAppDefaultare entry points for ZIO applications. - Runtime —
Runtime[R]is capable of executing tasks within an environmentR. - Exit —
Exit[E, A]describes the result of executing anIOvalue. - Cause —
Cause[E]is a description of a full story of a fiber failure.
Control Flow​
- Control Flow — ZIO's built-in conditional and looping combinators:
when,unless,cond,ifZIO,loop,iterate,foreach, andforeachPar.
Scheduling​
- Schedule —
Schedule[-Env, -In, +Out]is a composable, time-aware state machine for repeating and retrying ZIO effects with full control over timing, delays, and recurrence logic.
Contextual Data Types​
- ZEnvironment —
ZEnvironment[R]is a built-in type-level map for theZIOdata type which is responsible for maintaining the environment of aZIOeffect. - ZLayer —
ZLayer[-RIn, +E, +ROut]is a recipe to build an environment of typeROut, starting from a valueRIn, and possibly producing an errorEduring creation.- RLayer —
RLayer[-RIn, +ROut]is a type alias forZLayer[RIn, Throwable, ROut], which represents a layer that requiresRInas its input, it may fail withThrowablevalue, or returnsROutas its output. - ULayer —
ULayer[+ROut]is a type alias forZLayer[Any, Nothing, ROut], which represents a layer that doesn't require any services as its input, it can't fail, and returnsROutas its output. - Layer —
Layer[+E, +ROut]is a type alias forZLayer[Any, E, ROut], which represents a layer that doesn't require any services, it may fail with an error type ofE, and returnsROutas its output. - URLayer —
URLayer[-RIn, +ROut]is a type alias forZLayer[RIn, Nothing, ROut], which represents a layer that requiresRInas its input, it can't fail, and returnsROutas its output. - TaskLayer —
TaskLayer[+ROut]is a type alias forZLayer[Any, Throwable, ROut], which represents a layer that doesn't require any services as its input, it may fail withThrowablevalue, and returnsROutas its output.
- RLayer —
State Management​
- ZState— It models a state that can be read from and written to during the execution of an effect.
- Ref—
Ref[A]models a mutable reference to a value of typeA. - FiberRef—
FiberRef[A]models a mutable reference to a value of typeA. As opposed toRef[A], a value is bound to an executingFiberonly. You can think of it as Java'sThreadLocalon steroids. - ThreadLocalBridge— A service that synchronizes
FiberRefvalues with JavaThreadLocalvariables for seamless interoperability with legacy code and libraries that rely on thread-local storage.
Concurrency​
Fiber Primitives​
- Fiber — A fiber value models an
IOvalue that has started running, and is the moral equivalent of a green thread. - Fiber.Status —
Fiber.Statusdescribe the current status of a Fiber. - FiberId —
FiberIddescribe the unique identity of a Fiber.
Concurrency Primitives​
- Hub — A
Hubis an asynchronous message hub that allows publishers to efficiently broadcast values to many subscribers. - Promise — A
Promiseis a model of a variable that may be set a single time, and awaited on by many fibers. - Queue — A
Queueis an asynchronous queue that never blocks, which is safe for multiple concurrent producers and consumers. - Semaphore — A
Semaphoreis an asynchronous (non-blocking) semaphore that plays well with ZIO's interruption. - Ref —
Ref[A]models a mutable reference to a value of typeA. The two basic operations areset, which fills theRefwith a new value, andget, which retrieves its current content. All operations on aRefare atomic and thread-safe, providing a reliable foundation for synchronizing concurrent programs. - Ref.Synchronized —
Ref.Synchronized[A]models a mutable reference to a value of typeAin which we can store immutable data, and update it atomically and effectfully.
Synchronization Aids​
- ReentrantLock— The
ReentrantLockis a synchronization tool that is useful for synchronizing blocks of code. - CountdownLatch — A synchronization aid that allows one or more fibers to wait until a set of operations being performed in other fibers completes.
- CyclicBarrier — A synchronization aid that allows a set of fibers to all wait for each other to reach a common barrier point.
- ConcurrentMap — A Map wrapper over
java.util.concurrent.ConcurrentHashMap - ConcurrentSet — A Set implementation over
java.util.concurrent.ConcurrentHashMap
STM​
- STM — An
STMrepresents an effect that can be performed transactionally resulting in a failure or success. - TArray — A
TArrayis an array of mutable references that can participate in transactions. - TSet — A
TSetis a mutable set that can participate in transactions. - TMap — A
TMapis a mutable map that can participate in transactions. - TRef — A
TRefis a mutable reference to an immutable value that can participate in transactions. - TPriorityQueue — A
TPriorityQueueis a mutable priority queue that can participate in transactions. - TPromise — A
TPromiseis a mutable reference that can be set exactly once and can participate in transactions. - TQueue — A
TQueueis a mutable queue that can participate in transactions. - TReentrantLock — A
TReentrantLockis a reentrant read / write lock that can be composed. - TSemaphore — A
TSemaphoreis a semaphore that can participate in transactions.
Resource Management​
- Cached — A container for a possibly resourceful value that is loaded into memory and can be refreshed either manually or automatically according to a schedule.
- Scope — A scope in which resources can safely be used.
- ScopedRef — A resourceful mutable reference that automatically manages acquisition and release of scoped resources.
- ZPool — An asynchronous and concurrent generalized pool of reusable resources.
Streaming​
- ZStream —
ZStreamis a lazy, concurrent, asynchronous source of values.- Stream —
Stream[E, A]is a type alias forZStream[Any, E, A], which represents a ZIO stream that does not require any services, and may fail with anE, or produce elements with anA.
- Stream —
- ZSink —
ZSinkis a consumer of values from aZStream, which may produce a value when it has consumed enough.- Sink —
Sink[InErr, A, OutErr, L, B]is a type alias forZSink[Any, InErr, A, OutErr, L, B].
- Sink —
- ZPipeline —
ZPipelineis a polymorphic stream transformer. - SubscriptionRef —
SubscriptionRef[A]contains a current value of typeAand a stream that can be consumed to observe all changes to that value.
Logging​
- ZLogger —
ZLogger[-Message, +Output]is ZIO's pure functional logging interface. It can be composed, filtered, and fan-out across multiple destinations without global mutable state.
Metrics​
IO supports 5 types of Metrics:
- Counter — The Counter is used for any value that increases over time like request counts.
- Gauge — The gauge is a single numerical value that can arbitrary goes up or down over time like memory usage.
- Histogram — The Histogram is used to track the distribution of a set of observed values across a set of buckets like request latencies.
- Summary — The Summary represents a sliding window of a time series along with metrics for certain percentiles of the time series, referred to as quantiles like request latencies.
- Frequency — The Frequency is a metric that counts the number of occurrences of distinct string values.
Testing​
- Spec— A
Spec[R, E]is the backbone of ZIO Test. All specs require an environment of typeRand may potentially fail with an error of typeE. - Assertion— An
Assertion[A]is a test assertion that can be used to assert the predicate of typeA => Boolean. - TestAspect— A
TestAspectis an aspect that can be weaved into specs. We can think of an aspect as a polymorphic function, capable of transforming one test into another. - Gen— A
Gen[R, A]represents a generator of values of typeA, which requires an environmentR. - Test Service— ZIO Test has the following out-of-the-box test services:
- TestConsole— It allows testing of applications that interact with the console.
- TestClock— We can deterministically and efficiently test effects involving the passage of time without actually having to wait for the full amount of time to pass.
- TestRandom— This service allows us having fully deterministic testing of code that deals with Randomness.
- TestSystem— It supports deterministic testing of effects involving system properties.
- Live— It provides access to the live environment from within the test environment for effects.