7

Is there a method in Akka (or in the standard library in Scala 2.10) to convert a Future[A] which might fail into a Future[Either[Exception,A]]? I know that you can write

f.map(Right(_)).recover {
  case e:Exception => Left(e)
}

It just seems to be such a common task that I wonder whether I have overlooked something. I'm interested in answers for Scala 2.9/Akka and Scala 2.10.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142
  • 1
    Sympathetic back story at https://groups.google.com/d/topic/scala-sips/OVz9WSJF1HY/discussion and https://groups.google.com/d/topic/scala-sips/VRlDgX4yFKE/discussion anyway. – som-snytt Jan 05 '13 at 07:49
  • I don't understand the purpose of that signature. Now your Future can have a Throwable in both the Success(Left()) case and the Failure() case. – Viktor Klang Jan 05 '13 at 11:28
  • 1
    @ViktorKlang: You could make the same argument against Option. Now your value has two ways of not existing: `None` and `Some(null)`. But in practice it is understood that we don't ever create a `Some(null)` just like we wouldn't create a `Future[Either...]` which could realistically fail (other than due to OOME or that kind of thing). – Kim Stebel Jan 05 '13 at 16:38
  • @KimStebel Future[Either[Throwable,T]] is about as useful as Some(null)? Sounds about right – Viktor Klang Jan 05 '13 at 18:44
  • @ViktorKlang: Haha, very funny. This isn't Twitter though. – Kim Stebel Jan 05 '13 at 21:45

3 Answers3

13

The primary reason why this method is missing is that it does not really have good semantics: the static type Future[Either[Throwable, T]] does not ensure that that future cannot fail, hence the type change does not gain you much in general.

It can of course make sense if you control all the code which handles those futures, and in that case it is trivial to add it yourself (the name is due to me posting before first coffee, feel free to replace with something better):

implicit class FutureOps[T](val f: Future[T]) extends AnyVal {
  def lift(implicit ec: ExecutionContext): Future[Either[Throwable,T]] = {
    val p = promise[Either[Throwable,T]]()
    f.onComplete {
      case Success(s)  => p success Right(s)
      case Failure(ex) => p success Left(ex)
    }
    p.future
  }
}

It works very similarly with Akka 2.0 futures, hence I leave that exercise to the reader.

Roland Kuhn
  • 15,412
  • 2
  • 36
  • 45
  • What is wrong with my implementation, except that it doesn't put the method into an implicit class? – Kim Stebel Jan 05 '13 at 10:21
  • Your implementation creates two additional futures while mine creates only one, so it is just an optimization. – Roland Kuhn Jan 05 '13 at 18:20
  • This is the alternative I meant in my comment to the other answer, but as a nod to Roland's lift, I wrote mine after midnight and called it `packaged` because people talk about packaging the exception; and I had just reviewed the old thread about the axed either method and was sensitive to any name with either in it. This is an apparently simple exercise in API design that is not so simple; I hope it shows up in a summary blog post for that reason. – som-snytt Jan 06 '13 at 01:22
4

Another version of such conversion (in standard Scala):

f.transform(tryResult => Success(tryResult.toEither))
Mikhail Golubtsov
  • 6,285
  • 3
  • 29
  • 36
0

I don't think you would want to do this anyway. Akka 2.0.5's docs show this for akka.dispatch.Future:

abstract def onComplete[U](func: (Either[Throwable, T]) ⇒ U): Future.this.type

So the information that the Future might fail is already embedded into the behavior of a Future[T]. The same applies with Scala 2.10's futures, where a future can complete as a Try[T] which is similar in purpose to an Either[Exception, T].

//in scala.concurrent.Future:
abstract def onComplete[U]
  (func: (Try[T]) ⇒ U)(implicit executor: ExecutionContext): Unit
Dylan
  • 13,645
  • 3
  • 40
  • 67
  • onComplete won't work for me, because my method needs to return a future. – Kim Stebel Jan 05 '13 at 05:52
  • In the sense in which everything is onComplete, an alternative to map and recover is to cut/paste transform to an extension method but amending the failure to success. – som-snytt Jan 05 '13 at 08:02
  • You can still return a Future. My point is that you don't need to do the transformation from `Future[A]` to `Future[Either[Exception, A]]` as it is redundant. – Dylan Jan 05 '13 at 16:42