Created
December 16, 2010 15:01
-
-
Save paolo-losi/743489 to your computer and use it in GitHub Desktop.
continuations and exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile with $scalac -P:continuations:enable test_exception.scala | |
// execute with $scala Test | |
import scala.util.continuations._ | |
object Test { | |
type WithExc[A] = Either[Exception, A] | |
def main(args: Array[String]) { | |
bar(); | |
} | |
def bar(): Unit = { | |
reset { | |
try { | |
val i: Int = shiftE { (k: Int=>Unit) => foo(k) } | |
println("i"+i) | |
} catch { | |
case e: Exception => println("got exception in bar!") | |
} | |
} | |
} | |
def shiftE[A](f: (A=>Unit) => Unit) = { | |
val rl: WithExc[A] = shift { wrapper(f) } | |
rl match { | |
case Left(e) => throw(e) | |
case Right(i) => i | |
} | |
} | |
def wrapper[A](f: (A => Unit) => Unit)(k: WithExc[A] => Unit): Unit = { | |
try { | |
f(i => k(Right(i))) | |
} catch { | |
case e:Exception => { println("exception catched in wrapper") | |
k(Left(e)) } | |
} | |
} | |
def foo(k: Int=>Unit ): Unit = { | |
println ("ok cont") | |
//throw new Exception("foo") | |
k(1) | |
println("j") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment