#Futures in Scala 2.12 (part 4)
This is the fourth of several posts describing the evolution of scala.concurrent.Future
in Scala 2.12.x
.
For the previous post, click here.
##Missing canonical combinators: transformWith
As we saw in the previous post, transform
provides a nice unification of both map
and recover
on Future
, and I know what you're thinking now: "What about flatMap
and recoverWith
?"
Say no more! transformWith
is the answer, and has the following lovely signature:
def transformWith[S](f: Try[T] => Future[S])(implicit executor: ExecutionContext): Future[S]
Remember to use the solution with the least power which will suffice for the task at hand, so prefer to use the flatMap
s and the recoverWith
s primarily, opting for the transformWith
as required.
And, before you say anything, yes, flatMap
and recoverWith
are implemented in terms of transformWith
in Scala 2.12!
EDIT:
Here's flatMap
:
def flatMap[S](f: T => Future[S])(implicit executor: ExecutionContext): Future[S] = transformWith {
case Success(s) => f(s)
case Failure(_) => this.asInstanceOf[Future[S]] //Safe cast to reuse current, failed, Future
}
And here's recoverWith
:
def recoverWith[U >: T](pf: PartialFunction[Throwable, Future[U]])(implicit executor: ExecutionContext): Future[U] =
transformWith {
case Failure(t) => pf.applyOrElse(t, (_: Throwable) => this) //Pass along current failure if no match
case Success(_) => this
}
###Benefits:
- Ultimate power, for when you require it
Here's the RSS feed of this blog and—as I love feedback—please share your thoughts.
To comment on the blog post itself, click here or here and comment on the PR.
Click here for the next part in this blog series.
Cheers, √