Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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_Future_For Comprehension - Fatal编程技术网

Scala 一种更好的语法,用于从用于理解的

Scala 一种更好的语法,用于从用于理解的,scala,future,for-comprehension,Scala,Future,For Comprehension,我有许多函数返回一个未来,这是一个需要理解的过程的结果,但是我需要从一些可能的失败中恢复。标准语法似乎将理解的过程捕获为中间结果,如下所示: def fooBar(): Future[String] = { val x = for { x <- foo() y <- bar(x) } yield y x.recover { case SomeException() => "bah" } } def fooBar():Future[Str

我有许多函数返回一个未来,这是一个需要理解的过程的结果,但是我需要从一些可能的失败中恢复。标准语法似乎将理解的过程捕获为中间结果,如下所示:

def fooBar(): Future[String] = {
  val x = for {
    x <- foo()
    y <- bar(x)
  } yield y
  x.recover {
    case SomeException() => "bah"
  }
}
def fooBar():Future[String]={
val x=用于{

x一些大括号调整有帮助,尽管对于多行表达式,有些人更喜欢大括号而不是括号:

scala> def f = (
     |   for {
     |     x <- foo;
     |     y <- bar(x)
     |   } yield y
     | ) recover {
     |   case _: NullPointerException => -1
     | }
f: scala.concurrent.Future[Int]
您可以使用所有syntaxy:

object Test extends App {
  import concurrent._
  import duration.Duration._
  import ExecutionContext.Implicits._
  type ~>[A, B] = PartialFunction[A, B]
  type NPE = NullPointerException
  class `recovering future`[A, R >: A](val f: Future[A], val pf: Throwable ~> R) {
    def map[B >: A <: R](m: A => B) = new `recovering future`[B, R](f map m, pf)
    def flatMap[B >: A <: R](m: A => Future[B]) = new `recovering future`[B, R](f flatMap m, pf)
    def recovered: Future[R] = f recover pf
  }
  object `recovering future` {
    implicit def `back to the future`[A, R >: A](x: `recovering future`[A, R]): Future[R] = x.recovered
  }
  implicit class `inline recoverer`[A](val f: Future[A]) {
    def recovering[B >: A](pf: Throwable ~> B) = new `recovering future`(f, pf)
  }

  def f = Future(8)
  def g(i: Int) = Future(42 + i)
  def e(i: Int): Future[Int] = Future((null: String).length)
对象测试扩展应用程序{
并发导入_
导入duration.duration_
导入ExecutionContext.Implicits_
type~>[A,B]=部分函数[A,B]
类型NPE=NullPointerException
类“恢复未来”[A,R>:A](val f:future[A],val pf:Throwable~>R){
定义映射[B>:ab)=新的“恢复未来”[B,R](f映射m,pf)
def flatMap[B>:未来[B])=新的“恢复未来”[B,R](f flatMap m,pf)
def已恢复:未来[R]=f恢复pf
}
对象“正在恢复未来”{
隐式def`back to the future`[A,R>:A](x:`recovering future`[A,R]):future[R]=x.recovered
}
隐式类`inline recoverer`[A](val f:Future[A]){
def正在恢复[B>:A](pf:Throwable~>B)=新的“正在恢复的未来”(f,pf)
}
def f=未来(8)
定义g(i:Int)=未来(42+i)
def e(i:Int):Future[Int]=Future((null:String).length)
朴素的:

  for {
    x <- f
    y <- g(x)
  } Console println y   // 50
用于{
这就可以了

def fooBar():Future[String]={
foo flatMap bar recover{case someException=>…}


“bar”将作用于“foo”返回的未来并生成所需的未来,而recover block可以像往常一样处理异常。

implicit def
返回未来(
:))
object Test extends App {
  import concurrent._
  import duration.Duration._
  import ExecutionContext.Implicits._
  type ~>[A, B] = PartialFunction[A, B]
  type NPE = NullPointerException
  class `recovering future`[A, R >: A](val f: Future[A], val pf: Throwable ~> R) {
    def map[B >: A <: R](m: A => B) = new `recovering future`[B, R](f map m, pf)
    def flatMap[B >: A <: R](m: A => Future[B]) = new `recovering future`[B, R](f flatMap m, pf)
    def recovered: Future[R] = f recover pf
  }
  object `recovering future` {
    implicit def `back to the future`[A, R >: A](x: `recovering future`[A, R]): Future[R] = x.recovered
  }
  implicit class `inline recoverer`[A](val f: Future[A]) {
    def recovering[B >: A](pf: Throwable ~> B) = new `recovering future`(f, pf)
  }

  def f = Future(8)
  def g(i: Int) = Future(42 + i)
  def e(i: Int): Future[Int] = Future((null: String).length)
  for {
    x <- f
    y <- g(x)
  } Console println y   // 50
  def compute: Future[Int] =
    for {
      x <- f recovering { case _: NPE => -1 }
      y <- g(x)
    } yield y
  Console println (Await result (compute, Inf))  // 50
  def fail: Future[Int] =
    for {
      x <- f recovering { case _: NPE => -1 }
      y <- e(x)
    } yield y
  Console println (Await result (fail, Inf))  // -1
}