Either in Cats vs Either in Scala

Reading Time: 4 minutes

Hola amigos! Last time I discussed Monads in general and in Cats as well. In this blog, let’s have a look at one of the most known monad Either. I’ll be giving an introduction to it and also there will be a comparison between two i.e Either in Cats vs Either in Scala.

What is Either?

Either basically represents a value of one of two possible types. An Either is either a Left or Right. Right shows the correct value and Left shows some failure. It can be used where there is a possibility of having missing values. It is useful as not only it handles missing values/ failures but it can also give some message for the failure and that way we can know the reason as well.

Either in Scala

Here you can have a look at how we use Either in Scala.

One thing to notice is that the return type of Right.apply and Left.apply is Right and Left respectively instead of Either.

In the above case, the return type is Either because Right and Left are children of Either and their common type will be Either. Here the value ‘either’ will be:

Here we were able to wrap the exception that would have occurred on division by zero. This is an advantage of using Either.

Either in Cats

Cats provide us with useful Monad instances and one of them is Either. You can import cats.syntax.either and it will provide you with many extension methods. Even in Cats, one can use Left and Right directly, but there is a much better way for the same. After importing cats.syntax.either._ we get .asRight and .asLeft methods. They can be considered as “Smart Constructors” as they can be used to wrap a value in Right and Left respectively but the return type will be Either.

Look at the examples here:

You can compare this with the previous example which is using Right.apply and Left.apply and you will see the difference in return type.

Problem due to Return type

Let’s look at a scenario where we can face some problem while using Either in Scala.

Why is this happening? The compiler infers the type of accumulator as Right and expects Right in both the cases but in else part return type is Left. And, the problem arises there.

How can we resolve this?

The extension methods provided in Cats for Either also resolve the problem discussed above.

On using .asRight and .asLeft, the compiler infers the type as Either[String, Int] and it gets the same type in both the condition.

Yipppeeee ! problem solved.

Is Either right-biased in both cases?

What is the meaning of right-biased? It means that most of the methods work directly on Right value. Examples of those methods are map and flatMap.

From Scala version 2.12 and above Either is right-biased. In Cats you wont even have to import cats.syntax.either._ in this case.

Important Note: If you are using Scala version 2.11 or less than in Either in Scala you won’t be able to use map/flatMap directly rather you will have to call .right each time but in Either in Cats you can directly use them just by importing cats.syntax.either._.

Are there any extension methods?

Standard Scala Library does not provide with extension methods but Cats Library does.

On importing we get multiple methods. One of them is “ensure” which allow us to check if the right value satisfies a predicate. If not then it will return value of Left type.

There are many more and they will be discussed in my next blog. Stay tuned for that. 😊

Summary

  • In Scala we don’t have so-called “Smart Constructors” but they are available in Cats for Either.
  • Left.apply and Right.apply has a return type of Left and Right respectively in standard Scala library and Cats as well but having .asLeft and .asRight gives an upper-hand to Either in Cats.
  • In Cats Library we have many extension methods(extra methods) that can make the work easier and presents you with different methods which is not the case in standard Scala Library.

Reference

Knoldus-blog-footer-image

Written by 

Muskan Gupta is a Software Consultant having almost 1-year experience. She has knowledge of languages like C, C++, Java and Scala. She is familiar with Object-Oriented Programming Paradigms and also has an interest in relational database technologies. Her hobbies include travelling, reading novels and listening to music.

Leave a Reply