Is it correct that reset requires shift inside the block? I tried it and got the following:
scala> reset {}
error: cannot cps-transform expression (): type arguments [Unit,Unit,Nothing]
do not conform to method shiftUnit's type parameter bounds [A,B,C >: B]
It looks reasonable (since reset block without shift inside is “dead code”, which is never executed) but I do not understand the error.
What is the exact meaning of the error message?
I don’t agree, that code within
resetis dead withoutshift. Actuallyresetjust defines the boundaries of a continuation (that’s because they are called delimited continuations).The code would be dead if you haveshiftsomewhere withinresetand you do not call continuation function. For example:The code after
shiftis dead (println(3)) because I have not calledk(Unit).From the other hand, seems that
resetexpects some special return type from it’s body – the one that annotated with@cpsParamannotation. You can check definition ofresetmethod:And
shiftproduces just whatresetmethod expects. Here is definition ofshiftmethod:But you still can use
resetwithoutshiftcall within it. This trick will do it:Please note, that
@cpsis just type alias for@cpsParam. Here it’s definition: