summaryrefslogtreecommitdiff
path: root/src/continuations/library
diff options
context:
space:
mode:
authorTiark Rompf <tiark.rompf@epfl.ch>2010-03-16 08:19:59 +0000
committerTiark Rompf <tiark.rompf@epfl.ch>2010-03-16 08:19:59 +0000
commit166c496d5784d0be3611b270d9eba39f1273e048 (patch)
treebb52feed6cd9221f98d36edf9da303336098ad89 /src/continuations/library
parent324eeff963585af9248885473beeabfe8770ed68 (diff)
downloadscala-166c496d5784d0be3611b270d9eba39f1273e048.tar.gz
scala-166c496d5784d0be3611b270d9eba39f1273e048.tar.bz2
scala-166c496d5784d0be3611b270d9eba39f1273e048.zip
added support for continuations in try/catch bl...
added support for continuations in try/catch blocks. review by community.
Diffstat (limited to 'src/continuations/library')
-rw-r--r--src/continuations/library/scala/util/continuations/ControlContext.scala85
-rw-r--r--src/continuations/library/scala/util/continuations/package.scala2
2 files changed, 71 insertions, 16 deletions
diff --git a/src/continuations/library/scala/util/continuations/ControlContext.scala b/src/continuations/library/scala/util/continuations/ControlContext.scala
index 709d2d2b02..9ed83a7e71 100644
--- a/src/continuations/library/scala/util/continuations/ControlContext.scala
+++ b/src/continuations/library/scala/util/continuations/ControlContext.scala
@@ -14,21 +14,39 @@ private class cpsMinus extends Annotation // implementation detail
-@serializable final class ControlContext[+A,-B,+C](val fun: (A => B) => C, val x: A) {
+@serializable final class ControlContext[+A,-B,+C](val fun: (A => B, Throwable => B) => C, val x: A) {
- final def map[A1](f: (A => A1)): ControlContext[A1,B,C] = {
- if (fun eq null)
- new ControlContext(null, f(x))
- else
+ /*
+ final def map[A1](f: A => A1): ControlContext[A1,B,C] = {
new ControlContext((k:(A1 => B)) => fun((x:A) => k(f(x))), null.asInstanceOf[A1])
- }
+ }
- /*
final def flatMap[A1,B1<:B](f: (A => ControlContext[A1,B1,B])): ControlContext[A1,B1,C] = {
new ControlContext((k:(A1 => B1)) => fun((x:A) => f(x).fun(k)))
}
*/
+
+ final def map[A1](f: A => A1): ControlContext[A1,B,C] = {
+ if (fun eq null)
+ new ControlContext(null, f(x))
+ else
+ new ControlContext({ (k: A1 => B, thr: Throwable => B) =>
+ fun( { (x:A) =>
+ var done = false
+ try {
+ val res = f(x)
+ done = true
+ k(res)
+ } catch {
+ case ex if !done =>
+ thr(ex)
+ }
+ }, thr)
+ }, null.asInstanceOf[A1])
+ }
+
+
// it would be nice if @inline would turn the trivial path into a tail call.
// unfortunately it doesn't, so we do it ourselves in SelectiveCPSTransform
@@ -36,25 +54,62 @@ private class cpsMinus extends Annotation // implementation detail
if (fun eq null)
f(x).asInstanceOf[ControlContext[A1,B1,C]]
else
- new ControlContext({ k:(A1 => B1) =>
- fun { (x:A) =>
- val ctxR = f(x)
- val res: C1 = ctxR.foreach(k)
- res
- }
+ new ControlContext({ (k: A1 => B1, thr: Throwable => B1) =>
+ fun( { (x:A) =>
+ var done = false
+ try {
+ val ctxR = f(x)
+ done = true
+ val res: C1 = ctxR.foreachFull(k, thr) // => B1
+ res
+ } catch {
+ case ex if !done =>
+ thr(ex).asInstanceOf[B] // => B NOTE: in general this is unsafe!
+ } // However, the plugin will not generate offending code
+ }, thr.asInstanceOf[Throwable=>B]) // => B
}, null.asInstanceOf[A1])
}
- final def foreach(f: (A => B)) = {
+ final def foreach(f: A => B) = foreachFull(f, throw _)
+
+ def foreachFull(f: A => B, g: Throwable => B): C = {
if (fun eq null)
f(x).asInstanceOf[C]
else
- fun(f)
+ fun(f, g)
}
+
final def isTrivial = fun eq null
final def getTrivialValue = x.asInstanceOf[A]
// need filter or other functions?
+ final def flatCat[A1>:A,B1<:B,C1>:C<:B1](pf: PartialFunction[Throwable, ControlContext[A1,B1,C1]]): ControlContext[A1,B1,C1] = {
+ if (fun eq null)
+ this
+ else {
+ val fun1 = (ret1: A1 => B1, thr1: Throwable => B1) => {
+ val thr: Throwable => B1 = { t: Throwable =>
+ var captureExceptions = true
+ try {
+ if (pf.isDefinedAt(t)) {
+ val cc1 = pf(t)
+ captureExceptions = false
+ cc1.foreachFull(ret1, thr1) // Throw => B
+ } else {
+ captureExceptions = false
+ thr1(t) // Throw => B1
+ }
+ } catch {
+ case t1 if captureExceptions => thr1(t1) // => E2
+ }
+ }
+ foreachFull(ret1, thr)// fun(ret1, thr) // => B
+ }
+ new ControlContext(fun1, null.asInstanceOf[A1])
+ }
+ }
+
+ // TODO: finally
}
diff --git a/src/continuations/library/scala/util/continuations/package.scala b/src/continuations/library/scala/util/continuations/package.scala
index 0d924565c1..1fa89da2b4 100644
--- a/src/continuations/library/scala/util/continuations/package.scala
+++ b/src/continuations/library/scala/util/continuations/package.scala
@@ -55,7 +55,7 @@ package object continuations {
}
def shiftR[A,B,C](fun: (A => B) => C): ControlContext[A,B,C] = {
- new ControlContext(fun, null.asInstanceOf[A])
+ new ControlContext((f:A=>B,g:Throwable=>B) => fun(f), null.asInstanceOf[A])
}
def reifyR[A,B,C](ctx: => ControlContext[A,B,C]): ControlContext[A,B,C] = {