cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sylvain Wallez <sylvain.wal...@anyware-tech.com>
Subject The granularity of continuations
Date Sun, 28 Sep 2003 16:27:29 GMT
Hi all,

Investingating in the fancy uses that can be made of continuations, I 
was reading again the RhinoWithContinuations page on the wiki [1], and I 
felt confused by an example in that page :

 function someFunction()  {
     var kont  = new  Continuation();
     print("captured: " + kont);
     return kont;
  }

  var k = someFunction();
  if (k instanceof Continuation) {
     print("k is a continuation");
     k(200);
  } else {
     print("k is now a " + typeof(k));
  }
  print(k);

Whose result is :
  captured: [object Continuation]
  k is a continuation
  k is now a number
  200

Why is "captured: [...]" printed only once, even if located _after_ the 
"new Continuation()" statement which I supposed to take the snapshot of 
the current execution state ?

The anwser is (Chris, please confirm or correct me if this is wrong) 
that "new Continuation()" doesn't take a snapshot of the exact location 
of the statement, but a snapshot of the closest function call, i.e. 
"someFunction()" in that case. And when the continuation is called using 
"k(200)", execution restarts at the location where someFunction() was 
called, with the new "200" result. Thus why we don't see "captured: 
[...]" twice.

So the granularity of a continuation is the _function call_, and not the 
statement.

Consider now this :
  function someFunction() {
      var kont1 = new Continuation();
      print("captured 1");
      var kont2 = new Continuation();
      print("captured 2");
      return kont1; // or kont2 !!
  }
Returning kont1 or kont2 doesn't matter, as the execution will restart 
at the call point of someFunction(). Confusing...

Digging further on continuations, I found an interesting comment to a 
weblog [2], where the guy proposes to extend the "arguments" object to 
hold the continuation associated to a function, i.e. replace :
    var kont = new Continuation();
by :
    var kont = arguments.continuation;

IMO, this syntax removes the confusion outlined above, since it 
unambiguously attaches the continuation to the current function call, 
and thus matches better the actual granularity of a continuation.

What do you think ? Would it be difficult to implement it that way ?

Sylvain

[1] http://wiki.cocoondev.org/Wiki.jsp?page=RhinoWithContinuations
[2] http://lambda.weblogs.com/discuss/msgReader$8089#8093

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Mime
View raw message