ZIO is a library that allows us to build modern applications that are asynchronous, concurrent, resilient, efficient, easy to understand and to test, using the principles of functional programming.
Why do we say that ZIO allows us to build applications that are easy to understand and test? Because it helps us to build applications of any complexity incrementally, through a combination of descriptions of interactions with the outside world. By the way, these descriptions are called functional effects.
Why do we say that ZIO allows us to build applications that are resilient? Because ZIO takes full advantage of the Scala type system, in such a way that it can catch more bugs at compile time, rather than at run time. This is great because, just by looking at the signature of a function, we can tell:
And finally, why do we say that ZIO allows us to build applications that are asynchronous and concurrent? Because ZIO gives us the superpowers to work with asynchronous and concurrent programming, using a fiber-based model, which is much more efficient than a thread-based model. We will not go into much detail about this particular aspect in this article, however it is worth mentioning that it is precisely in this area that ZIO shines, allowing us to build really performant applications.
The most important data type in the ZIO library (and also the basic building block of any application based on this library), is also called ZIO:
ZIO [-R, +E, +A]
The ZIO data type is a functional effect, which means that it is an immutable value that contains a description of a series of interactions with the outside world (database queries, calls to third-party APIs, etc.). A good mental model of the ZIO data type is the following:
R => Either[E, A]
This means that a ZIO effect:
It’s worth mentioning that ZIO provides some type aliases for the ZIO data type which are very useful when it comes to representing some common use cases: