diff options
author | Paul Phillips <paulp@improving.org> | 2012-04-02 13:18:08 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-04-02 13:18:08 -0700 |
commit | 40334826eb573eed527cbd396ab406e7376edbcc (patch) | |
tree | 706a862b6e38f18ac9658ef79492d857d85d4b46 /src/library/scala/concurrent/Future.scala | |
parent | 03f32bd22f85bf3a6f8824dbe4a7a989e717c071 (diff) | |
parent | 9452d939331ee28313f0c9b2de260ee32d46af27 (diff) | |
download | scala-40334826eb573eed527cbd396ab406e7376edbcc.tar.gz scala-40334826eb573eed527cbd396ab406e7376edbcc.tar.bz2 scala-40334826eb573eed527cbd396ab406e7376edbcc.zip |
Merge branch 'develop'
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. * |