I just finished Programming in Scala, and I\'ve been looking into the changes between Scala 2.7 and 2.8. The one that seems to be the most important is the continua
I found the existing explanations to be less effective at explaining the concept than I would hope. I hope this one is clear (and correct.) I have not used continuations yet.
When a continuation function cf
is called:
shift
block and begins again at the end of it
cf
is what the shift
block "evaluates" to as execution continues. this can be different for every call to cf
reset
block (or until a call to reset
if there is no block)
reset
block (or the parameter to reset
() if there is no block) is what cf
returnscf
until the end of the shift
blockreset
block (or a call to reset?)So in this example, follow the letters from A to Z
reset {
// A
shift { cf: (Int=>Int) =>
// B
val eleven = cf(10)
// E
println(eleven)
val oneHundredOne = cf(100)
// H
println(oneHundredOne)
oneHundredOne
}
// C execution continues here with the 10 as the context
// F execution continues here with 100
+ 1
// D 10.+(1) has been executed - 11 is returned from cf which gets assigned to eleven
// G 100.+(1) has been executed and 101 is returned and assigned to oneHundredOne
}
// I
This prints:
11
101