Scala标准库中是否有从Either到Try的转换?也许我错过了一些东西,但我没有找到它们。
Either
Try
mccptt671#
(注意:此答案适用于Scala 2.11。有关Scala的最新版本,请参阅其他答案)据我所知,这在标准库中是不存在的。尽管Either通常用于Left失败和Right成功,它实际上是为了支持两种可能的返回类型的概念而设计的,其中一种不一定是失败的情况。Either并没有像Try那样被设计成一个成功/失败的monad。话虽如此,你可以很容易地丰富Either并添加这些转换。它可能看起来像这样:
Left
Right
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) }
字符串
xriantvc2#
在Scala 2.12.x中,Try有一个toEither方法:http://www.scala-lang.org/api/2.12.x/scala/util/Try.html#toEither:scala. util. Either [Throwable,T]
cczfrluj3#
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) } }
mdfafbf14#
答案取决于如何将Failure转换为Left(反之亦然)。如果你不需要使用异常的细节,那么Try可以通过Option的中间路线转换为Either:
Failure
Option
val tried = Try(1 / 0) val either = tried.toOption.toRight("arithmetic error")
字符串另一种方式的转换需要你构造一些Throwable。它可以像这样完成:
either.fold(left => Failure(new Exception(left)), right => Success(right))
型
rdlzhqv95#
从Scala 2.13开始(以及Scala 3),标准库支持这两种转换。Scala标准库中从Either到Try的转换在这里记录,反之亦然。
5条答案
按热度按时间mccptt671#
(注意:此答案适用于Scala 2.11。有关Scala的最新版本,请参阅其他答案)
据我所知,这在标准库中是不存在的。尽管
Either
通常用于Left
失败和Right
成功,它实际上是为了支持两种可能的返回类型的概念而设计的,其中一种不一定是失败的情况。Either
并没有像Try
那样被设计成一个成功/失败的monad。话虽如此,你可以很容易地丰富Either
并添加这些转换。它可能看起来像这样:字符串
xriantvc2#
在Scala 2.12.x中,Try有一个toEither方法:http://www.scala-lang.org/api/2.12.x/scala/util/Try.html#toEither:scala. util. Either [Throwable,T]
cczfrluj3#
字符串
mdfafbf14#
答案取决于如何将
Failure
转换为Left
(反之亦然)。如果你不需要使用异常的细节,那么Try
可以通过Option
的中间路线转换为Either
:字符串
另一种方式的转换需要你构造一些Throwable。它可以像这样完成:
型
rdlzhqv95#
从Scala 2.13开始(以及Scala 3),标准库支持这两种转换。Scala标准库中从
Either
到Try
的转换在这里记录,反之亦然。