Given these two methods that use Either
, in the second method I need to forward the error using Left(error) => Left(error)
. Is there a way to omit this in the second method (or use more elegant code) as the statement just needs to be passed through?
trait Error
case class ErrorClass (msg: String) extends Error
def intFunction(i:Int) : Either[ErrorClass,Int] = {
if (i>0)
Right(i)
else
Left(ErrorClass("error"))
}
def multiplier(j:Int) : Either[ErrorClass,Int] = {
val either = intFunction(2)
either match {
case Right(i) => Right(i*j)
case Left(error) => Left(error)
}
}