Exit e-book
Show all chapters
04
Introduction to the ZIO Library
04. 
Introduction to the ZIO Library

Sign up to our Newsletter

Signing up to our newsletter allows you to read all our ebooks.

    Introduction to Programming with ZIO Functional Effects
    04

    Introduction to the ZIO Library

    What is the ZIO Library for?

    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:

    • If it has external dependencies.
    • If it can fail or not, and also with what type of errors it can fail.
    • If it can finish successfully or not, and also what the type of data is that returns when finishing.

    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 ZIO data type

    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:

    • Needs a context of type R to run (this context can be anything: a connection to a database, a REST client , a configuration object, etc.).
    • It may fail with an error of type E or it may complete successfully, returning a value of type A.

    Common aliases for the ZIO data type

    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:

    • Task[+A] = ZIO[Any, Throwable, A]: This means a Task[A] is a ZIO effect that:
      • Doesn’t require an environment to run (that’s why the R type is replaced by Any, meaning the effect will run no matter what we provide to it as an environment)
      • Can fail with a Throwable
      • Can succeed with an A
    • UIO[+A] = ZIO[Any, Nothing, A]: This means a UIO[A] is a ZIO effect that:
      • Doesn’t require an environment to run.
      • Can’t fail
      • Can succeed with an A
    • RIO[-R, +A] = ZIO[R, Throwable, A]: This means a RIO[R, A] is a ZIO effect that:
      • Requires an environment R to run
      • Can fail with a Throwable
      • Can succeed with an A
    • IO[+E, +A] = ZIO[Any, E, A]: This means a IO[E, A] is a ZIO effect that:
      • Doesn’t require an environment to run.
      • Can fail with an E
      • Can succeed with an A
    • URIO[-R, +A] = ZIO[R, Nothing, A]: This means a URIO[R, A] is a ZIO effect that:
      • Requires an environment R to run
      • Can’t fail
      • Can succeed with an A
    PREVIOUS
    Chapter
    03
    NEXT
    Chapter
    05