From f584d243487dcd1214291167707e2f53fef5ab5e Mon Sep 17 00:00:00 2001 From: Tiark Rompf Date: Thu, 11 Mar 2010 16:55:38 +0000 Subject: moved the continuations plugin into trunk. --- .../library/scala/util/continuations/package.scala | 65 ++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/continuations/library/scala/util/continuations/package.scala (limited to 'src/continuations/library') diff --git a/src/continuations/library/scala/util/continuations/package.scala b/src/continuations/library/scala/util/continuations/package.scala new file mode 100644 index 0000000000..0d924565c1 --- /dev/null +++ b/src/continuations/library/scala/util/continuations/package.scala @@ -0,0 +1,65 @@ +// $Id$ + + +// TODO: scaladoc + +package scala.util + +package object continuations { + + type cps[A] = cpsParam[A,A] + + type suspendable = cps[Unit] + + + def shift[A,B,C](fun: (A => B) => C): A @cpsParam[B,C] = { + throw new NoSuchMethodException("this code has to be compiled with the Scala continuations plugin enabled") + } + + def reset[A,C](ctx: =>(A @cpsParam[A,C])): C = { + val ctxR = reify[A,A,C](ctx) + if (ctxR.isTrivial) + ctxR.getTrivialValue.asInstanceOf[C] + else + ctxR.foreach((x:A) => x) + } + + def reset0[A](ctx: =>(A @cpsParam[A,A])): A = reset(ctx) + + def run[A](ctx: =>(Any @cpsParam[Unit,A])): A = { + val ctxR = reify[Any,Unit,A](ctx) + if (ctxR.isTrivial) + ctxR.getTrivialValue.asInstanceOf[A] + else + ctxR.foreach((x:Any) => ()) + } + + + // methods below are primarily implementation details and are not + // needed frequently in client code + + def shiftUnit0[A,B](x: A): A @cpsParam[B,B] = { + shiftUnit[A,B,B](x) + } + + def shiftUnit[A,B,C>:B](x: A): A @cpsParam[B,C] = { + throw new NoSuchMethodException("this code has to be compiled with the Scala continuations plugin enabled") + } + + def reify[A,B,C](ctx: =>(A @cpsParam[B,C])): ControlContext[A,B,C] = { + throw new NoSuchMethodException("this code has to be compiled with the Scala continuations plugin enabled") + } + + def shiftUnitR[A,B](x: A): ControlContext[A,B,B] = { + new ControlContext(null, x) + } + + def shiftR[A,B,C](fun: (A => B) => C): ControlContext[A,B,C] = { + new ControlContext(fun, null.asInstanceOf[A]) + } + + def reifyR[A,B,C](ctx: => ControlContext[A,B,C]): ControlContext[A,B,C] = { + ctx + } + +} -- cgit v1.2.3