www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - PROPOSAL: goto-out-of-delegates = continuations

Back in January, I proposed a "goto-out-of-delegates" feature.  At the 
time, I didn't see a need for it except as a possible alternative to 
exceptions.  Now I've come to realize that what I proposed is 
essentially continuations, or, to be more specific, something MORE 
GENERAL than continuations.

Wikipedia has a better description 
(http://en.wikipedia.org/wiki/Continuation), but in summary, a 
continuation can be thought of as a delegate that, when it is called, 
breaks out of the current call stack, unwraps it, and continues 
execution at the point where the continuation was created.  But with my 
goto-out-of-delegates proposal, you can continue *anywhere* in the 
original function, not just at the point immediately after the delegate 
was created.  Some languages allow a continuation to be called multiple 
times (my proposal would allow this trivially).

Open question: would this type of continuation make any sense if the 
creating function exited?  If we wanted to allow it, what exactly would 
a "return" mean from a continuation?

The idea is to make this syntax legal:

PROPOSED CODE
   void foo()
   {
     void NestedFunc(int i)
     {
       if(i == 0)
         goto LABEL_1;
       else
         goto LABEL_2;
     }

     <do stuff>

     bar(&NestedFunc);

     <if bar() returns without ever calling the delegate,
      then we run here>

   LABEL_1:
     <if bar() calls dg(0), then the dg goto's here>

   LABEL_2:
     <if bar() calls dg(1), then the dg goto's here>
   }
END CODE
Apr 21 2008