Scala标准库中是否有Either
到Try
的转换,反之亦然?也许我错过了一些东西,但我找不到它们。
答案 0 :(得分:17)
据我所知,标准库中不存在这种情况。虽然Either
通常使用Left
是失败而Right
是成功的,但它实际上是为了支持两种可能的返回类型的概念,其中一种不一定是失败的情况。我猜测人们期望存在的这些转换不存在,因为Either
并非真正设计成像Try
那样的成功/失败单子。尽管如此,自己充实Either
并添加这些转换非常容易。这可能看起来像这样:
object MyExtensions {
implicit class RichEither[L <: Throwable,R](e:Either[L,R]){
def toTry:Try[R] = e.fold(Failure(_), Success(_))
}
implicit class RichTry[T](t:Try[T]){
def toEither:Either[Throwable,T] = t.transform(s => Success(Right(s)), f => Success(Left(f))).get
}
}
object ExtensionsExample extends App{
import MyExtensions._
val t:Try[String] = Success("foo")
println(t.toEither)
val t2:Try[String] = Failure(new RuntimeException("bar"))
println(t2.toEither)
val e:Either[Throwable,String] = Right("foo")
println(e.toTry)
val e2:Either[Throwable,String] = Left(new RuntimeException("bar"))
println(e2.toTry)
}
答案 1 :(得分:14)
import scala.util.{ Either, Failure, Left, Right, Success, Try }
implicit def eitherToTry[A <: Exception, B](either: Either[A, B]): Try[B] = {
either match {
case Right(obj) => Success(obj)
case Left(err) => Failure(err)
}
}
implicit def tryToEither[A](obj: Try[A]): Either[Throwable, A] = {
obj match {
case Success(something) => Right(something)
case Failure(err) => Left(err)
}
}
答案 2 :(得分:9)
在Scala 2.12.x中尝试使用toEither方法:http://www.scala-lang.org/api/2.12.x/scala/util/Try.html#toEither:scala.util.Either[Throwable,T]
答案 3 :(得分:5)
答案取决于如何将Failure
转换为Left
(反之亦然)。如果您不需要使用例外的详细信息,则可以通过Try
的中间路线将Either
转换为Option
:
val tried = Try(1 / 0)
val either = tried.toOption.toRight("arithmetic error")
转换另一种方式需要你构造一些Throwable。可以这样做:
either.fold(left => Failure(new Exception(left)), right => Success(right))