Full closures

BCS ao at pathlink.com
Mon Aug 25 14:38:52 PDT 2008


Reply to Bruno,

> BCS wrote:
> 
>> Reply to Bruno,
>> 
>>> BCS wrote:
>>> 
>>>> That is a good point, but how else you you get a real closure that
>>>> has mutable state?
>>>> 
>>>> int delegate() Seq()
>>>> {
>>>> int at = 0;
>>>> int Next() { return at++; }
>>>> return &Next;
>>>> }
>>> Err.... just have a closure like D has now?... one that allows
>>> access to any visible variable?
>>> 
>> Franks local delegates won't work because they aren't valid after the
>> other function returns, his heap delegates won't work because the
>> variables they can access can't be altered.
>> 
> I feel there is a misunderstanding here. I wasn't talking about
> Frank's closures, but the one D currently has. What exactly did you
> mean with "how else [do] you get a real closure that has mutable
> state?" ?
> 

The problem with the current ones is that they aren't valid in some cases 
if the outer function leave the scope the delegate is defined in.


void Foo()
{
   int delegate() Bar;
   for(int m = 3; m; m--)
   {
       const int i = m;
       Ber = {return i;}
   }
   {
       int j = 6;
       int k = Bar();
       //k could be 6
   }
}





More information about the Digitalmars-d mailing list