A Scala Either, Left, and Right example (like Option, Some, and None)

Summary: This post is a discussion of the “Option/Some/None Pattern” in Scala, along with how to use Either/Left/Right instead of Option when you need to know why something failed.

A Scala idom is to use the Option/Some/None pattern instead of using exceptions along with try/catch/finally (especially using Option/Some/None instead of using null values). While using Option is generally a much better approach, one 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, with a difference being that with Either 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 generally the intention, so as a practical matter 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 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 meaning “right,” as in “correct.” 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 from 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 its 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:

While I’m in the neighborhood, here are links to the Option and Some classes, and the None object:

In summary, I hope this discussion of Either, Left, and Right has been helpful.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.