I would like to initialize an Either
to Left
, but this requires specifying the type of the Right
side (or vice versa).
If I don't, then the Right
side is typed to Nothing
by default, and in order to do something such as:
List(5, 42, 7).foldLeft(Left("empty")) {
case (Left(_), i) => Right(i)
case (Right(s), i) => Right(s + i)
}
error: type mismatch;
found : scala.util.Right[Nothing,Int]
required: scala.util.Left[String,Nothing]
I'm obviously forced to verbosely provide the type of both sides of the Either
:
List(5, 42, 7).foldLeft(Left("empty"): Either[String, Int]) {
case (Left(_), i) => Right(i)
case (Right(s), i) => Right(s + i)
}
In a similar way, I can use Option.empty[Int]
to initialize None
as an Option[Int]
, would there be a way to initialize Left("smthg")
as an Either[String, Int]
?