Do not call get on an Either projection

When retrieving the content of an Either, do not use get on one of its projections.

Reason

Some projections are left projections of a Right, or right projections of a Left, and get deals with them by throwing an exception:

Left(1).right.get
// java.util.NoSuchElementException: Either.right.get on Left
// 	at scala.util.Either$RightProjection.get(Either.scala:640)
// 	at repl.Session$App$$anonfun$1.apply(either_projection_get.md:9)
// 	at repl.Session$App$$anonfun$1.apply(either_projection_get.md:9)
Right(1).left.get
// java.util.NoSuchElementException: Either.left.get on Right
// 	at scala.util.Either$LeftProjection.get(Either.scala:496)
// 	at repl.Session$App$$anonfun$2.apply(either_projection_get.md:17)
// 	at repl.Session$App$$anonfun$2.apply(either_projection_get.md:17)

If you have a default value to provide for the “other” case, use getOrElse on a projection:

Left(1).right.getOrElse(-1)
// res0: Int = -1

Alternatively, if you’re using the common convention of treating the Left side of an Either as the error case, you can also call getOrElse directly on the Either:

Left(1).getOrElse(-1)
// res1: Int = -1

Another practical approach is to use fold, which lets you provide a handler for each case:

(Left(1): Either[Int, Boolean]).fold(
  i => s"Found an int: '$i'",
  b => s"Found a boolean: '$b'"
)
// res2: String = "Found an int: '1'"

Checked by

LinterRule
Scapegoat
  • EitherGet
WartRemover