Summary
ZIO is a library for asynchronous and concurrent programming that is based on pure functional programming.
For background on how pure functional programming deals with effects like input and output, see the Background section.
At the core of ZIO is ZIO
, a powerful effect type inspired by Haskell's IO
monad. This data type lets you solve complex problems with simple, type-safe, testable, and composable code.
ZIO
The ZIO[R, E, A]
data type has three type parameters:
R
- Environment Type. The effect requires an environment of typeR
. If this type parameter isAny
, it means the effect has no requirements, because you can run the effect with any value (for example, the unit value()
).E
- Failure Type. The effect may fail with a value of typeE
. Some applications will useThrowable
. If this type parameter isNothing
, it means the effect cannot fail, because there are no values of typeNothing
.A
- Success Type. The effect may succeed with a value of typeA
. If this type parameter isUnit
, it means the effect produces no useful information, while if it isNothing
, it means the effect runs forever (or until failure).
For example, an effect of type ZIO[Any, IOException, Byte]
has no requirements, may fail with a value of type IOException
, or may succeed with a value of type Byte
.
A value of type ZIO[R, E, A]
is like an effectful version of the following function type:
R => Either[E, A]
This function, which requires an R
, might produce either an E
, representing failure, or an A
, representing success. ZIO effects are not actually functions, of course, because they model complex effects, like asynchronous and concurrent effects.
Type Aliases
The ZIO
data type is the only effect type in ZIO. However, there are a family of type aliases and companion objects that simplify common cases:
UIO[A]
— This is a type alias forZIO[Any, Nothing, A]
, which represents an effect that has no requirements, and cannot fail, but can succeed with anA
.URIO[R, A]
— This is a type alias forZIO[R, Nothing, A]
, which represents an effect that requires anR
, and cannot fail, but can succeed with anA
.Task[A]
— This is a type alias forZIO[Any, Throwable, A]
, which represents an effect that has no requirements, and may fail with aThrowable
value, or succeed with anA
.RIO[R, A]
— This is a type alias forZIO[R, Throwable, A]
, which represents an effect that requires anR
, and may fail with aThrowable
value, or succeed with anA
.IO[E, A]
— This is a type alias forZIO[Any, E, A]
, which represents an effect that has no requirements, and may fail with anE
, or succeed with anA
.
These type aliases all have companion objects, and these companion objects have methods that can be used to construct values of the appropriate type.
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 type built into Scala's standard library.
If you are using Cats Effect libraries, you may find the RIO
type useful, since it allows you to thread environments through third-party libraries and your application.
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.
Next Steps
If you are comfortable with the ZIO data type, and its family of type aliases, the next step is learning how to create effects.