diff options
author | Aleksandar Prokopec <axel22@gmail.com> | 2012-03-29 17:52:08 +0200 |
---|---|---|
committer | Aleksandar Prokopec <axel22@gmail.com> | 2012-03-29 17:52:08 +0200 |
commit | 7c84f1fc32e99a309f9b47b7359764aecbfc21e6 (patch) | |
tree | b6584e2fdfff279ca34644fc7e54f6fa88ac9b9f /src/library/scala/concurrent/Future.scala | |
parent | 802162b5b1260bde50aafdc5ae9534c54472b109 (diff) | |
download | scala-7c84f1fc32e99a309f9b47b7359764aecbfc21e6.tar.gz scala-7c84f1fc32e99a309f9b47b7359764aecbfc21e6.tar.bz2 scala-7c84f1fc32e99a309f9b47b7359764aecbfc21e6.zip |
Add withFilter and mapTo to futures.
Diffstat (limited to 'src/library/scala/concurrent/Future.scala')
-rw-r--r-- | src/library/scala/concurrent/Future.scala | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index d73801aa90..8cecadc605 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -274,6 +274,18 @@ self => p.future } + /** Used by for-comprehensions. + */ + final def withFilter(p: T => Boolean): Future[T] = filter(p) + // final def withFilter(p: T => Boolean) = new FutureWithFilter[T](this, p) + + // final class FutureWithFilter[+S](self: Future[S], p: S => Boolean) { + // def foreach(f: S => Unit): Unit = self filter p foreach f + // def map[R](f: S => R) = self filter p map f + // def flatMap[R](f: S => Future[R]) = self filter p flatMap f + // def withFilter(q: S => Boolean): FutureWithFilter[S] = new FutureWithFilter[S](self, x => p(x) && q(x)) + // } + /** Creates a new future by mapping the value of the current future if the given partial function is defined at that value. * * If the current future contains a value for which the partial function is defined, the new future will also hold that value. @@ -417,7 +429,26 @@ self => p.future } - + + /** Creates a new `Future[S]` which is completed with this `Future`'s result if + * that conforms to `S`'s erased type or a `ClassCastException` otherwise. + */ + def mapTo[S](implicit m: Manifest[S]): Future[S] = { + val p = newPromise[S] + + onComplete { + case l: Failure[_] => p complete l.asInstanceOf[Try[S]] + case Success(t) => + p complete (try { + Success(impl.Future.boxedType(m.erasure).cast(t).asInstanceOf[S]) + } catch { + case e: ClassCastException => Failure(e) + }) + } + + p.future + } + /** Applies the side-effecting function to the result of this future, and returns * a new future with the result of this future. * |