Website | Source | Reference

ZIO is a next-generation framework for building cloud-native applications on the JVM. With a beginner-friendly yet powerful functional core, ZIO lets developers quickly build best-practice applications that are highly scalable, testable, robust, resilient, resource-safe, efficient, and observable.

At the heart of ZIO is a powerful data type called ZIO, which is the fundamental building block for every ZIO application.

ZIO

The ZIO data type is called a functional effect, and represents a unit of computation inside a ZIO application. Similar to a blueprint or a workflow, functional effects are precise plans that describe a computation or interaction. When executed by the ZIO runtime system, a functional effect will either fail with some type of error, or succeed with some type of value.

Like the List data type, the ZIO data type is a generic data type, and uses type parameters for improved type-safety. The List data type has a single type parameter, which represents the type of element that is stored in the List. The ZIO data type has three type parameters: ZIO[R, E, A].

The type parameters of the ZIO data type have the following meanings:

As several examples of how to interpret the types of ZIO effects:

An effect of type ZIO[Any, IOException, Byte] has no requirements, and when executed, such an effect may fail with a value of type IOException, or may succeed with a value of type Byte.

An effect of type ZIO[Connection, SQLException, ResultSet] requires a Connection, and when executed, such an effect may fail with a value of type SQLException, or may succeed with a value of type ResultSet.

An effect of type ZIO[HttpRequest, HttpFailure, HttpSuccess] requires an HttpRequest, and when executed, such an effect may fail with a value of type HttpFailure, or may succeed with a value of type HttpSuccess.

The environment type parameter is a composite type parameter, because sometimes, a single effect can require multiple values of different types. If you see that an effect has a type of ZIO[UserSession with HttpRequest, E, A] (Scala 2.x) or ZIO[UserSession & HttpRequest, E, A] (Scala 3.x), it means that the effect requires multiple contextual values before it can be executed.

Although this analogy is not precise, a ZIO effect can be thought of as a function:

R => Either[E, A]

This function requires an R and produces a failure of type E or a success value of type A.

ZIO effects are not actually functions, of course, because they model complex computations and interactions, which may be asynchronous, concurrent, or resourceful.

Type Aliases

The ZIO data type is the only effect type in ZIO. However, there are a family of type aliases that reduce the need to type:

Tips For Getting Started With Type Aliases

If you are new to functional effects, we recommend starting with the Task type, which has a single type parameter and corresponds most closely to the Future data types built into the Scala and Java standard libraries.

If you are using Cats Effect libraries, you may find the RIO type useful, since it allows you to thread context through third-party libraries.

No matter what type alias you use in your application, UIO can be useful for describing infallible effects, including those resulting from handling all errors.

Finally, if you are an experienced functional programmer, then direct use of the ZIO data type is recommended, although you may find it useful to create your own family of type aliases in different parts of your application.


Tags: library   jvm   functional  

Last modified 07 October 2024