Cool Trick: Currying for stack-on-heap style delegates

Russell Lewis webmaster at villagersonline.com
Tue Sep 4 13:18:53 PDT 2007


As everybody knows, delegate literals currently use a pointer to the 
stack frame as their "this" pointer, meaning that they cannot be 
returned from a function, or used after the frame exits.  Some of us 
have argued for a "copy the stack frame" syntax.  I'm stumbled upon a 
pretty cool trick to accomplish the same thing (although it can get 
tedious when you draw too many variables into the delegate).  The trick 
is to declare a function literal instead of a delegate literal, and then 
use a Curry() template to pass stack variables into the function:

void delegate() myFunc()
{
   int a,b,c;
   ....
   return Curry(a,b,c, function void(int a,int b,int c) {
                         // put your code here
                       });
}



Obviously, this gets a little ugly as you get more and more variables, 
particularly as the types get complex.  I've been pondering better 
syntax possibilities, using the keyword "curry."  A couple of ideas that 
I had were:


DECLARATION FORM (short, but only useful for bringing in variables 
declared in the outer scope):

return delegate void() {
     curry a; // declare that 'a' from the outer scope should be passed
              // as a curried argument to this delegate
   }

EXPRESSION FORM (more flexible):

return delegate void() {
     auto a = curry a; // the expression "curry a" resolves to the
                       // reading of a hidden, curried argument which
                       // gets its value from the outer scope
   }

To clarify, the EXPRESSION FORM example above would be equivalent to:

return Curry(a, void function(int _temp) {
                   auto a = _temp;
                 });



More information about the Digitalmars-d mailing list