D doesn't have real closures

BCS ao at pathlink.com
Thu Sep 13 13:35:25 PDT 2007


Reply to Russell,

> I think that closures could be easily implemented using
> compiler-driven
> currying.  I'm thinking that we should add the syntax
> curry <exp>
> which would be valid inside delegate and function literals, and which
> is
> defined to mean:
> 
[...]

I like this idea.

This would cover most cases where you want to have a persistent delegate. 
However it wouldn't cover the cases where the action is expected to modify 
function state. However most of the time you wont want to do that in a persistent 
delegate. What is more likely is that you will want to create two delegates 
with shared persistent state. But then you might as well generate a struct. 
Which, come to think of it, get back to the Idea I have always liked; delegate 
literals from arbitrary scope.

void fn()
{
   struct S
   {
     int i;
     int j;
     int at;
   };

   S* s = new S  // darn I want anon structs
   alias s this; // might this work?

   i = 5;
   j = 10;

   Funky(s.{at =  i;}, s.{at++; return at > j;} s.{return at;}); //make three 
delegates that uses 's' as context ptr.
}





More information about the Digitalmars-d-announce mailing list