D2 full closure?

Frank Benoit keinfarbton at googlemail.com
Mon Jul 14 15:59:27 PDT 2008


Jason House schrieb:
> Frank Benoit Wrote:
> 
>> With DMD 2.015:
>>
>> alias void delegate() Runner;
>> void main(){
>>    Runner[] runner;
>>    for( int i = 0; i < 3; i++ ){
>>      const int ci = i;
>>      runners ~= delegate(){
>>        writefln( "run with ci=%d", ci );
>>      }
>>    }
>>    foreach( runner; runners ){
>>      runner();
>>    }
>> }
>>
>>
>> Output:
>> run with ci=2
>> run with ci=2
>> run with ci=2
>>
>> Is this output expected?
>>  From the technical point yes, because i know how it is implemented.
>> But is this expected in the sense of "Full closure"?
>>
>> In Java, if ci would be "final" and the delegate an anonymous class,
>> the output would be 0,1,2.
> 
> I could have sworn there was a bug report to make this work like you want. Another alternative is to use bind. 

Yes i know there are ways to make it work.
But my question is, shouldn't it work in this code?
Is it a bug or not?
Is it ok that the constant is changing the value?
Should each delegate get its own heap allocated copy of the stack frame?



More information about the Digitalmars-d mailing list