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?
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.
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.
|Provides the effect of latency|
|Try[T]||Provides the effect of managing exceptions|
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
Like everything else,
ZIO treats services as an effect. As any service may require dependency
[RIn] and produces an effect
For sake of better understanding, we will look into a small application that will help us understand
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.
WordCounterservice takes the content of the file and produces a map with words which has the total occurrence of the word.
- Here the
SourceReaderservice 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()converts instantiation of service to functional effect which return
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[SourceReader.Service, WordCounter.Service] makes sure
SourceReader is available for the construction of
To build a single service that requires numerous services. We use
ZLayer.fromServices() which is similar to
To check out my application click here.
To know about the basics of ZIO check my previous blog by clicking here.
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.