Introduction to Delimited Continuations/Using shift and reset
Appearance
Let's rewrite run
using Scala's delimited continuations primitives, shift
and reset
.
def run() = reset {
println("What is your name?")
val name = shift { k: (String => Unit) =>
val name = readln()
k(name)
}
println("Hello, " + name + "!")
}
The flow of the program has not changed. Printing the second message to the screen becomes a continuation of type String => Unit
, which is passed into the shift
block as the function k
, to be invoked with the user's input.
|