In this post we’ll look at Scalaz Either
. This is Scalaz’s version of the standard Scala Either
. Before we look at Scalaz Either
, we’ll look at Scala Either
.
Scala Either
Let’s begin by going over the docs:[1]
Represents a value of one of two possible types (a disjoint union.) Instances of Either are either an instance of Left or Right.
…
Convention dictates that Left is used for failure and Right is used for success.
With the definition out of the way, let’s look at some code. The example is modeled around the code in the official Scala Either
docs.
Creating an Either
1 | @ def parseInt(str: String): Either[String, Int] = { |
Next, let’s create a case each of success and failure.
1 | @ val success = parseInt("2") |
Using a for
Comprehension
Scala Either
is not a monad and so you cannot use it in a for
comprehensions.
1 | @ for { |
NOTE:
Previously, Scala Either
was not a monad so it couldn’t be used in for
comprehensions. Now, it is a monad and can be used in for
comprehensions.
Scalaz Either
A valid question to ask is why would one use Scalaz Either
when Scala Either
is a monad. The answer is that Scalaz Either
is a lot more convenient and powerful compared to Scala Either
. Let’s begin by refactoring parseInt
to return a Scalaz Either
.
Creating an Either
1 | @ import scalaz._ |
Next, let’s create a case each of success and failure.
1 | @ val success = parseInt("2") |
The return type of our function is indicated by String \/ Int
. This means we may return a String
on the left in case of failure and an Int
on the right in case of success. We create right or left projections by calling right
or left
, respectively, and mentioning the type of the value that will be on the other side. For example, we call right[String]
because the left side is a String
. The right projection is indicated by \/-
and left projection is indicated by -\/
.
Using a for
Comprehension
1 | @ for { |
Because Scalaz Either
is also a monad, it can be used in a for
comprehension.
Checking Left or Right
1 | @ success isRight |
Akin to Scala Either
, Scalaz Either
also lets you check for left or right by calling isLeft
or isRight
, respectively.
Ternary Operator
1 | @ success ? "YES" | "NO" |
Scalaz Either
provides you with a getOrElse
which you can use to as a ternary operator using its symbolic representation |
.
Folding an Either
1 | @ success fold( |
Both Scala and Scalaz Either
provide you with a fold
method which run the first function if we have a left, or the second function if we have a right.
Converting to Validation
The single biggest difference between Scala Either
and Scalaz Either
is that Scalaz Either
can be converted to other types like Validation
, etc. For example, converting an Either
to a Validation
allows you to accumulate errors. As the code comments state:
A \/ B
is also isomorphic to Validation[A, B]
. The subtle but important difference is that Applicative
instances for Validation
accumulates errors (“lefts”)
1 | @ 1.right validation |
We create a Validation
by calling validation
method on the Either
instance. Depending on a left or right, we get either a Success
or Failure
.
Conclusion
Scalaz Either
and Scala Either
are pretty similar in the latest version of Scala (2.12, as of writing). Which one you decide to use depends upon your personal preference. My preference is to use Scalaz Either
throughout my code if I am using other Scalaz features to maintain consistency.