Shivangi Gupta shows off the Either
keyword in Scala:
How to get values from Either?
There are many ways we will talk about all one by one. One way to get values is by doing left and right projection. We can not perform any operation i.e, map, filter etc; on Either. Either provide left and right methods to get the left and right projection. Projection on either allows us to apply functions like map, filter etc.
For example,
scala> val div = divide(14, 7) div: scala.util.Either[String,Int] = Right(2) scala> div.right res1: scala.util.Either.RightProjection[String,Int] = RightProjection(Right(2))When we applied right on either, it returned RightProjection. Now we can extract the value from right projection using get, but if there is no value the compiler will blow up using get.
There’s more to Scala exception handling than just try-catch.