A single use continuation
is a continuation which (duh) can only be used once (or not at all); after which it becomes invalidated. This restriction greatly simplifies the implementation (no need for CactusStack
s, heap-allocated ActivationRecord
s, or other things like that), but restricts their use. Full continuations can be used multiple times, which makes them powerful (but easily-abused). See ContinuationsAreGotos
, an argument (rant) that mainly applies to FirstClass
In many (most) cases, invocation of such a continuation is up the call-stack only (or the call-chain if ActivationRecord
s aren't kept on TheStack
One way to enforce the single-use constraint is to make continuations a second-class language feature; allow them to be passed as arguments to functions (and invoked), but not to be bound to (non-temporary) variables or returned from functions (or referred to in constructs that create a new thread)
. In this way, it's impossible
to use a continuation more than once--the instant a continuation is invoked; control is transferred to a context where the continuation is no longer in scope. The restriction on sticking the continuation in a variable prevents it from being invoked a second time; the restriction from being returned ensures that invokation always goes up the call-stack.
Despite these restrictions, SingleUseContinuation
s are useful. They can be used to model ExceptionHandling
(the terminating kind found in CeePlusPlus
, and others), BackTracking
/failure semantics, as well as ordinary function calls and basic control structures (looping and iteration). They can't model CoRoutine
s/threads, iterators/generators, etc. however.
setjmp()/longjmp() can be used to implement single-use continuations in CeeLanguage (but arguably not well)
. See ContinuationsInCee
Does any language have continuations which are single-use, but allow the continuation to be invoked if the invoking context has already returned "normally"?
The ActorsModel has continuations that are intermediate in expressiveness between SingleUseContinuations, and full continuations as in SchemeLanguage. Normally they are single-use, but an actor can be programmed to accept a continuation more than once. So "actor continuations" are nearest to what this question asks for, I think. See
"Viewing Control Structures as Patterns of Passing Messages" and
"Issues in the Design and Implementation of Act2", at http://www.erights.org/history/actors.html .
There is a paper in the book 'Advanced LISP Technology' called 'Indefinite One-time Continuations'.
The authors (Komiya and Yuasa) have implemented them for TUTScheme. The idea is
similar to SingleUseContinuation