Scala 如何从未来左右移动
我的职能如下:Scala 如何从未来左右移动,scala,Scala,我的职能如下: def bar(x : Int) : Either[String, Future[Option[Foo]]] = { Goo() recover { case e => Left("Some error string") } } 如您所见,如果未来失败,那么它将影响恢复体内的部分功能。这将返回Left并满足任一类型的左边部分。我一直坚持的是,如果美好的未来成功的话,如何归还权利 我尝试了以下方法: def bar(x : Int) : Either[String, F
def bar(x : Int) : Either[String, Future[Option[Foo]]] = {
Goo() recover { case e => Left("Some error string") }
}
如您所见,如果未来失败,那么它将影响恢复体内的部分功能。这将返回Left并满足任一类型的左边部分。我一直坚持的是,如果美好的未来成功的话,如何归还权利
我尝试了以下方法:
def bar(x : Int) : Either[String, Future[Option[Foo]]] = {
Goo().map(x => Right(Future.successful(x))) recover { case e => Left("Some error string") }
}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Foo(n: Int)
/** Takes a string and returns a future int-parsing of it */
def Goo(s: String): Future[Foo] = Future {
Foo(java.lang.Integer.parseInt(s)) // will throw an exception on non-int
}
但是,我得到一个类型错误,表明bar
的返回类型是Future[String,Future[Foo]]]
如何返回Right(x)
其中x
是Foo
类型的某个值
更新
def bar(x : Int) : Future[Either[String, Option[Foo]]] = {
Goo().map(x => Right(x)) recover { case e => Left("Some error string") }
}
您没有定义
Goo
,但我现在假设如下:
def bar(x : Int) : Either[String, Future[Option[Foo]]] = {
Goo().map(x => Right(Future.successful(x))) recover { case e => Left("Some error string") }
}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Foo(n: Int)
/** Takes a string and returns a future int-parsing of it */
def Goo(s: String): Future[Foo] = Future {
Foo(java.lang.Integer.parseInt(s)) // will throw an exception on non-int
}
然后,如果希望bar(s:String)
返回String,Option[Foo]]
其中选项是Some[Foo]
如果数字是可解析且为正的,或者None
如果可解析但为非正的,并且字符串是解析失败原因的解释,则可以执行以下操作:
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.control.NonFatal
def bar(s: String): Future[Either[String, Option[Foo]]] = {
Goo(s).map { foo: Foo =>
Right(if (foo.n > 0) Some(foo) else None)
}.recover {
case NonFatal(e) => Left("Failed to parse %s: %s".format(s, e))
}
}
瞧
scala> Await.result(bar("4"), Duration.Inf)
res1: Either[String,Option[Foo]] = Right(Some(Foo(4)))
scala> Await.result(bar("-4"), Duration.Inf)
res2: Either[String,Option[Foo]] = Right(None)
scala> Await.result(bar("four"), Duration.Inf)
res3: Either[String,Option[Foo]] = Left(Failed to parse four: java.lang.NumberFormatException: For input string: "four")
您没有定义Goo
,但我现在假设如下:
def bar(x : Int) : Either[String, Future[Option[Foo]]] = {
Goo().map(x => Right(Future.successful(x))) recover { case e => Left("Some error string") }
}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
case class Foo(n: Int)
/** Takes a string and returns a future int-parsing of it */
def Goo(s: String): Future[Foo] = Future {
Foo(java.lang.Integer.parseInt(s)) // will throw an exception on non-int
}
然后,如果希望bar(s:String)
返回String,Option[Foo]]
其中选项是Some[Foo]
如果数字是可解析且为正的,或者None
如果可解析但为非正的,并且字符串是解析失败原因的解释,则可以执行以下操作:
import scala.concurrent.Await
import scala.concurrent.duration.Duration
import scala.util.control.NonFatal
def bar(s: String): Future[Either[String, Option[Foo]]] = {
Goo(s).map { foo: Foo =>
Right(if (foo.n > 0) Some(foo) else None)
}.recover {
case NonFatal(e) => Left("Failed to parse %s: %s".format(s, e))
}
}
瞧
scala> Await.result(bar("4"), Duration.Inf)
res1: Either[String,Option[Foo]] = Right(Some(Foo(4)))
scala> Await.result(bar("-4"), Duration.Inf)
res2: Either[String,Option[Foo]] = Right(None)
scala> Await.result(bar("four"), Duration.Inf)
res3: Either[String,Option[Foo]] = Left(Failed to parse four: java.lang.NumberFormatException: For input string: "four")
顺便说一句,如果没有wait
,就无法获得未来的或。-您只能获得未来的或@dk14,请查看更新。将退货类型更改为“未来”[要么…,现在类型检查,实际上是我想要的。你可能不需要未来。成功的
,顺便说一句,没有等待
,就无法获得未来的要么
-你只能获得未来的未来
,请查看更新。将返回类型更改为未来[要么…现在输入检查,实际上就是我想要的。你可能不需要未来。成功的
在那里继续,抛出java.lang.Thread.sleep(2000)
作为未来{…}中的第一件事Goo
的定义中的
可以看到实际的延迟期货。继续并抛出一个java.lang.Thread.sleep(2000)
,作为Goo
的定义中的第一件事,以查看实际的延迟期货。