next up previous contents
Next: Writing Your First SALSA Up: Coordinating Concurrency Previous: Join continuations   Contents


First-class continuations

The purpose of the 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 a recursive continuation code. In SALSA, the keyword currentContinuation is reserved for first-class continuations. To explain the effect of first-class continuations, we use two examples, with and without it, to show the difference. In the first example, statement 1 prints out "Hello World SALSA":
 
//The first example of usning First-Class Continuation
...
void saySomething() {
  standardOutput <- print("Hello ") @ 
  standardOutput <- print("World ") @ 
  currentContinuation;
}
....
//statement 1 in some method.
saySomething() @  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 First-Class Continuation
// statement 2 may produce a different result from 
// that of statement 1.
...
void saySomething() {
  standardOutput <- print("Hello ") @ 
  standardOutput <- print("World ");
}
....
//statement 2 inside some method:
saySomething() @  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 is meaningless. For instance, the following piece of code always prints out "Hello World", but "SALSA" never gets printed:
 
// The third example - with First-Class Continuation
// One should see "Hello World" in the standard output 
// after statement 3 is totally executed.
...
void saySomething() {
  boolean alwaysTrue=true;
  if (alwaysTrue) {
    standardOutput <- print("Hello ") @ 
    standardOutput <- print("World ") @ 
    currentContinuation;
  }
  standardOutput<-println("SALSA");
}
....
//statement 3 inside some method:
saySomething() @  standardOutput <- println() ;
 
   

next up previous contents
Next: Writing Your First SALSA Up: Coordinating Concurrency Previous: Join continuations   Contents
Wei-Jen Wang 2005-10-24