scala/scala — pull request #1056 by heathermiller (2012-08-04 23:01:35+00:00)
Title: Bases futures on Try instead of Either

This pull request is a result of meetings between the Scala team, the Akka team and Twitter Engineering to move toward a consolidated future implementation.

It's main contribution is the replacement of the use of `Either` with `scala.util.Try` in methods `Future.onComplete`, `Future.value`, `Future.andThen`, `Promise.complete`, and `Promise.tryComplete`.

`Try` is an abstraction adopted from the `com.twitter.util` package \[[1]\]. Unlike `Either`, `Try` is projection-less, which makes it more composable and more semantically similar to futures. Furthermore, as compared to `Either`, `Try` is better suited to handling exceptions, including execption-specific combinators such as `recover` and `recoverWith` (called `rescue` in `com.twitter.util.Try`). Moreover, both fatal and non-fatal exceptions are handled consistently across combinators such as `map` and `flatMap`.

Changes compared to Twitter's original implementation of `Try`:
- Use `scala.util.control.NonFatal` (adopted from Akka \[[2]\] for SIP-14) to avoid catching fatal exceptions.
- Rename `Return/Throw` (in package `com.twitter.util`) to `Success`/`Failure` (in package `scala.util`); this makes the use of `Try` in combination with `Future` more regular.
- Rename `handle` (in package `com.twitter.util`) to `recover` (in package `scala.util`).
- Rename `rescue` (in package `com.twitter.util`) to `recoverWith` (in package `scala.util`), after `scala.concurrent.Future`'s semantically-similar `recoverWith` method.
- Add `foreach` method.
- Add `orElse` method.
- Add `failed` projection.
- Add `transform` method.

This pull request additionally contains some small fixes (`flatMap` in `Try` now goes through the `NonFatal` extractor), clean ups, and added tests on `Try`.

@phaller has also included a `prepare` method on `ExecutionContext`s which enables abstractions to be built on top of execution contexts, such as Twitter's `Local` for handling data local to a chain of future callbacks, as previously discussed.

Review by @viktorklang, and if available @mariusaeriksen

[1]: https://github.com/twitter/util/ "com.twitter.util"
[2]: http://akka.io/ "Akka"