ZIO Monads, Effects, and ZLayer With Scala

Reading Time: 3 minutes

Overview

In this blog, we will talk about ZIO Effects and ZLayer. To recognize the fundamentals of ZIO do check out this blog. The ZIO library is centered on the ZIO type. Instances of ZIO are mentioned as “effects”, which describe something a software normally does: printing, computing, beginning connections, analyzing, writing, and lots of others. However, it’s really well worth stating that — similar to different IO monads — building such an “effect” does now not honestly produce it at that moment. as a substitute, a ZIO example is a facts shape describing an effect. Let’s in short speak approximately what’s monads?

ZIO Monads

In Scala, Monads is a construction that performs successive calculations. it is an object which covers the alternative object. it is worth noting that here, the output of an operation at some step is an input to other computations, which is a parent to the recent step of the program stated. Monad is neither a class nor a trait, it is a concept. The maximum collections of the Scala are Monads however not all the Monads are collections, several Monads might be containers like options in Scala. In short, we can say that in Scala the data types that implements map in addition to flatMap() like options, Lists, and so on. are referred to as Monads.

ZIO Effects

It is important to note from the outset that this is not about side effects. We are talking more about the main effect. Effects are what monads handle, and the main purpose of each monad is that effect.

For example, one of the functions of Scala is to put objects inside Option[T] to handle nullability. So for the Option[T] monad, the effect is to give an object or value an option. See the table below for more examples of the monads and their respective effects.

MonadEffect
Future[T]Provides the effect of latency
Try[T]Provides the effect of managing exceptions
Monads And Effect

ZLayer

A ZLayer[-RIn, +E, +ROut]represents an application layer; each layer in an application can require some services as input RIn and produces some services as output ROut.
Like everything else, ZIO treats services as an effect. As any service may require dependency [RIn] and produces an effect[ROut].
For sake of better understanding, we will look into a small application that will help us understand ZLayer.

The application is simple as it counts the number of words in a given list of URLs. We will split the application into two services:-

  • Let’s create our first service named SourceReader. This service opens the source then reads the content of a file and closes the sources.
  • WordCounter service takes the content of the file and produces a map with words which has the total occurrence of the word.
SouceReader.png
  • Here the SourceReader service does not require any resources therefore construction of the service layer is simple. We just need to wrap the constructor of service inside ZLayer.succeed(). ZLayer.succeed() converts instantiation of service to functional effect which return SourceReaderEnv.
WordCounter.png

WordCounter is different from SourceReader Service as it is dependent on it. Therefore SourceReader availability is important for the construction of WordCounter service. To help us in this we will require ZLayer.fromService(). ZLayer.fromService[SourceReader.Service, WordCounter.Service] makes sure SourceReader is available for the construction of WordCounter.

To build a single service that requires numerous services. We use ZLayer.fromServices() which is similar to ZLayer.fromService().

To check out my application click here.

To know about the basics of ZIO check my previous blog by clicking here.

Conclusion

We went through an overview of ZIO and we covered the essence of Monads, Effects and ZLayer, enough to understand what it does and how it can help us build independent services, which we can plug together to create complex applications.

Written by 

My name is Harshal Dubey. I am working as a Software Intern in AI/ML Studio at Knoldus. My hobbies are playing volleyball, football and Travelling.