Scala中的Try和Try

ix0qys7i  于 5个月前  发布在  Scala
关注(0)|答案(5)|浏览(53)

Scala标准库中是否有从EitherTry的转换?也许我错过了一些东西,但我没有找到它们。

mccptt67

mccptt671#

(注意:此答案适用于Scala 2.11。有关Scala的最新版本,请参阅其他答案)
据我所知,这在标准库中是不存在的。尽管Either通常用于Left失败和Right成功,它实际上是为了支持两种可能的返回类型的概念而设计的,其中一种不一定是失败的情况。Either并没有像Try那样被设计成一个成功/失败的monad。话虽如此,你可以很容易地丰富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)
}

字符串

xriantvc

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]

cczfrluj

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)
  }
}

字符串

mdfafbf1

mdfafbf14#

答案取决于如何将Failure转换为Left(反之亦然)。如果你不需要使用异常的细节,那么Try可以通过Option的中间路线转换为Either

val tried = Try(1 / 0)
val either = tried.toOption.toRight("arithmetic error")

字符串
另一种方式的转换需要你构造一些Throwable。它可以像这样完成:

either.fold(left => Failure(new Exception(left)), right => Success(right))

rdlzhqv9

rdlzhqv95#

从Scala 2.13开始(以及Scala 3),标准库支持这两种转换。Scala标准库中从EitherTry的转换在这里记录,反之亦然。

相关问题