next up previous contents
Next: Using Input/Output (I/O) Actors Up: Coordinating Concurrency Previous: Join Blocks


First-Class Continuations

The purpose of first-class continuations is to delegate computation to a third party, enabling dynamic replacement or expansion of messages grouped by token-passing continuations. First-class continuations are very useful for writing recursive code. In SALSA, the keyword currentContinuation is reserved for first-class continuations. To explain the effect of first-class continuations, we use two examples to show the difference. In the first example, statement 1 prints out "Hello World SALSA":
 
//The first example of using First-Class Continuations
...
void saySomething1() {
  standardOutput <- print("Hello ") @ 
  standardOutput <- print("World ") @ 
  currentContinuation;
}
....
//statement 1 in some method.
saySomething1() @  standardOutput <- print("SALSA"); 
 
   
In the following (the second) example, statement 2 may generate a different result from statement 1. It prints out either "Hello World SALSA", or "SALSAHello World ".
 
// The second example - without a First-Class Continuation
// Statement 2 may produce a different result from 
// that of Statement 1.
...
void saySomething2() {
  standardOutput <- print("Hello ") @ 
  standardOutput <- print("World ");
}
....
//statement 2 inside some method:
saySomething2() @  standardOutput <- print("SALSA") ;
 
   
The keyword currentContinuation has another impact on message passing -- the control of execution returns immediately after processing it. Any code after it will not be reached. For instance, the following piece of code always prints out "Hello World", but "SALSA" never gets printed:
 
// The third example - with a First-Class Continuation
// One should see "Hello World" in the standard output 
// after statement 3 is executed.
...
void saySomething3() {
  boolean alwaysTrue=true;
  if (alwaysTrue) {
    standardOutput <- print("Hello ") @ 
    standardOutput <- print("World ") @ 
    currentContinuation;
  }
  standardOutput<-println("SALSA");
}
....
//statement 3 inside some method:
saySomething3() @  standardOutput <- println() ;
 
   


next up previous contents
Next: Using Input/Output (I/O) Actors Up: Coordinating Concurrency Previous: Join Blocks
Wei-Jen Wang
2007-11-28