Best syntax for recovering from a for understanding

I have a number of functions that return a future that is the result of understanding, but I need to recover from some possible glitches on the way out. The standard syntax seems to capture comprehension for intermediate results as follows:

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

      

The best alternative I have found is to wrap the integer in parentheses for understanding:

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

      

This is more like a shortcut than a syntax improvement, so I'm wondering if there is a better way to weave the recovery for understanding?

+3


source to share


2 answers


Some curly brace adjustment helps, although some people prefer curly braces instead of parens for a multi-line expression:

scala> def f = (
     |   for {
     |     x <- foo;
     |     y <- bar(x)
     |   } yield y
     | ) recover {
     |   case _: NullPointerException => -1
     | }
f: scala.concurrent.Future[Int]

      

if you don't like

scala> foo flatMap bar recover { case _: NullPointerException => -1 }
res9: scala.concurrent.Future[Int] = scala.concurrent.impl.Promise$DefaultPromise@3efe7086

      

You can use all syntaxes:

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)

      

embellishment:



  for {
    x <- f
    y <- g(x)
  } Console println y   // 50

      

And with inlined recovery:

  def compute: Future[Int] =
    for {
      x <- f recovering { case _: NPE => -1 }
      y <- g(x)
    } yield y
  Console println (Await result (compute, Inf))  // 50

      

Or showing a bad case:

  def fail: Future[Int] =
    for {
      x <- f recovering { case _: NPE => -1 }
      y <- e(x)
    } yield y
  Console println (Await result (fail, Inf))  // -1
}

      

if you rock this way.

+9


source


It would do

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



"bar" will act on the Future returned by "foo" and print the requested future, and the restore block handles exceptions as usual.

+1


source







All Articles