summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-08-23 19:09:55 +0000
committerPaul Phillips <paulp@improving.org>2009-08-23 19:09:55 +0000
commitaffff809b09e3d23a2625080db3b0c7d9d8b1e0e (patch)
tree275ee4741bddc737457540f47d0b8b7de1a2f3c9 /src
parent50c1a4c2ad8044e7cc07fa9b2474790e15611c68 (diff)
downloadscala-affff809b09e3d23a2625080db3b0c7d9d8b1e0e.tar.gz
scala-affff809b09e3d23a2625080db3b0c7d9d8b1e0e.tar.bz2
scala-affff809b09e3d23a2625080db3b0c7d9d8b1e0e.zip
Tweaked a concurrent.ops method to adhere to th...
Tweaked a concurrent.ops method to adhere to the Either convention.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/concurrent/ops.scala32
-rw-r--r--src/library/scala/util/control/Exception.scala3
2 files changed, 15 insertions, 20 deletions
diff --git a/src/library/scala/concurrent/ops.scala b/src/library/scala/concurrent/ops.scala
index 3ce7de2465..182756dcb1 100644
--- a/src/library/scala/concurrent/ops.scala
+++ b/src/library/scala/concurrent/ops.scala
@@ -11,8 +11,8 @@
package scala.concurrent
-
import java.lang.Thread
+import scala.util.control.Exception.allCatch
/** The object <code>ops</code> ...
*
@@ -24,16 +24,14 @@ object ops {
TaskRunners.threadRunner
/**
- * If expression computed successfully return it in <code>Left</code>,
- * otherwise return exception in <code>Right</code>.
+ * If expression computed successfully return it in <code>Right</code>,
+ * otherwise return exception in <code>Left</code>.
*/
- private def tryCatch[A](left: => A): Either[A, Throwable] = {
- try {
- Left(left)
- } catch {
- case t => Right(t)
- }
- }
+ private def tryCatch[A](body: => A): Either[Throwable, A] =
+ allCatch[A] either body
+
+ private def getOrThrow[A](x: Either[Throwable, A]): A =
+ x.fold[A](throw _, identity _)
/** Evaluates an expression asynchronously.
*
@@ -48,12 +46,9 @@ object ops {
* @return ...
*/
def future[A](p: => A)(implicit runner: TaskRunner[Unit] = defaultRunner): () => A = {
- val result = new SyncVar[Either[A, Throwable]]
+ val result = new SyncVar[Either[Throwable, A]]
spawn({ result set tryCatch(p) })(runner)
- () => result.get match {
- case Left(a) => a
- case Right(t) => throw t
- }
+ () => getOrThrow(result.get)
}
/**
@@ -62,12 +57,9 @@ object ops {
* @return ...
*/
def par[A, B](xp: => A, yp: => B): (A, B) = {
- val y = new SyncVar[Either[B, Throwable]]
+ val y = new SyncVar[Either[Throwable, B]]
spawn { y set tryCatch(yp) }
- (xp, y.get match {
- case Left(b) => b
- case Right(t) => throw t
- })
+ (xp, getOrThrow(y.get))
}
/**
diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala
index 3b0590ca37..70c34015ac 100644
--- a/src/library/scala/util/control/Exception.scala
+++ b/src/library/scala/util/control/Exception.scala
@@ -143,6 +143,9 @@ object Exception
def apply(x: Throwable) = throw x
}
+ /** A Catch object which catches everything. */
+ final def allCatch[T]: Catch[T] = new Catch[T]({ case x => throw x }) withDesc "<everything>"
+
/** The empty Catch object. */
final val noCatch: Catch[Nothing] = new Catch(nothingCatcher) withDesc "<nothing>"