From 32e1da60a1ae55675b07e031bef89c7e28832d02 Mon Sep 17 00:00:00 2001 From: Tiark Rompf Date: Thu, 11 Mar 2010 20:36:43 +0000 Subject: added missing file from last commit. no review. --- .../scala/util/continuations/ControlContext.scala | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/continuations/library/scala/util/continuations/ControlContext.scala (limited to 'src/continuations/library') diff --git a/src/continuations/library/scala/util/continuations/ControlContext.scala b/src/continuations/library/scala/util/continuations/ControlContext.scala new file mode 100644 index 0000000000..709d2d2b02 --- /dev/null +++ b/src/continuations/library/scala/util/continuations/ControlContext.scala @@ -0,0 +1,60 @@ +// $Id$ + +package scala.util.continuations + + +class cpsParam[-B,+C] extends StaticAnnotation with TypeConstraint + +private class cpsSym[B] extends Annotation // implementation detail + +private class cpsSynth extends Annotation // implementation detail + +private class cpsPlus extends StaticAnnotation with TypeConstraint // implementation detail +private class cpsMinus extends Annotation // implementation detail + + + +@serializable final class ControlContext[+A,-B,+C](val fun: (A => 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 + 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))) + } + */ + + // 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 + + /*@inline*/ final def flatMap[A1,B1,C1<:B](f: (A => ControlContext[A1,B1,C1])): ControlContext[A1,B1,C] = { + 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 + } + }, null.asInstanceOf[A1]) + } + + final def foreach(f: (A => B)) = { + if (fun eq null) + f(x).asInstanceOf[C] + else + fun(f) + } + + final def isTrivial = fun eq null + final def getTrivialValue = x.asInstanceOf[A] + + // need filter or other functions? + +} -- cgit v1.2.3