Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何从未来左右移动_Scala - Fatal编程技术网

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
的定义中的第一件事,以查看实际的延迟期货。