top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Explain Either/Left/Right Design Pattern in Scala?

+1 vote
680 views

What is Either in Scala? What are Left and Right in Scala? Explain Either/Left/Right Design Pattern in Scala?

posted Aug 19, 2016 by Roshni

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

A Scala idom is to use the Option/Some/None pattern instead of using exceptions and try/catch/finally (especially using Option/Some/None instead of using null values), but a weakness of this approach is that the Option/Some/None approach doesn't tell you why something failed, that is, why you got a None instead of a Some.

Enter Either, with its companions Left and Right.

Update: Scala 2.10 introduced the Try, Success, and Failure classes. These may offer a better approach than the Either/Left/Right approach described in this article. At the very least, their names are more intuitive. See the Try class documentation for more information.

Either, Left, and Right

Either works just like Option, except you can return a String that describes the problem that occurred. Actually, what you do is wrap the String inside of Left. Actually, what you really do is return anything you want inside of Left, though returning information about the problem is the intent, so you typically return a String.

Here's a quick comparison of the Option and Either approaches:

Either is just like Option.
Right is just like Some.
Left is just like None, except you can include content with it to describe the problem.

Here's a look at how Either works in a very simple example:

object EitherLeftRightExample extends App {

  /**
   * A simple method to demonstrate how to declare that a method returns an Either,
   * and code that returns a Left or Right.
   */
  def divideXByY(x: Int, y: Int): Either[String, Int] = {
    if (y == 0) Left("Dude, can't divide by 0")
    else Right(x / y)
  }

  // a few different ways to use Either, Left, and Right
  println(divideXByY(1, 0))
  println(divideXByY(1, 1))
  divideXByY(1, 0) match {
    case Left(s) => println("Answer: " + s)
    case Right(i) => println("Answer: " + i)
  }

}

Discussion

In that example, the method divideXByY returns an Either, specifically this Either:

Either[String, Int]

In this example, the thing on the left is a String, and the thing on the right is an Int. Inside the function, the thing on the left is represented by Left, and the thing on the right is represented by Right.

Unless there's some part of computer lore I don't know about, I think Left and Right are unusual names, and the way I remember them is to think of Right as "right", as in "correct", i.e., "the answer". The only place left and right have a whole lot of meaning to me is in the declaration of Either[left-type, right-type], where Left is on the left, and Right is on the right.

If you're familiar with the Option/Some/None pattern, you can see that using Either is only a slight difference than using that pattern. Again, the intent of using Either is to pass a message back about what went wrong, so you can return the error message from an exception, or anything else you want to return with the Left instance.

In case nothing I've written makes any sense, here's a description of Either from the Either Scaladoc:

Represents a value of one of two possible types (a disjoint union.) Instances of Either are either an instance of Left or Right.

A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage, scala.None is replaced with a Left which can contain useful information. Right takes the place of Some. Convention dictates that Left is used for failure and Right is used for success.

The Scala Either, Left, and Right classes

I'll be glad to write more about this if anyone is interested, but for now, here are links to the Scala Either, Left, and Right classes:

Either
Left
Right

What the heck, here are links to the Option and Some classes, and the None object:

Option
Some
None
answer Aug 23, 2016 by Karthick.c
Similar Questions
+4 votes

What is the main use of Option and Either in Scala? How do we handle Errors or Exceptions in Functional Style(FP style) in Scala?

0 votes

Explain the main difference between List and Stream in Scala Collection API? How do we prove that difference? When do we choose Stream?

...