D2 full closure?
Jason House
jason.james.house at gmail.com
Mon Jul 14 19:35:37 PDT 2008
Frank Benoit wrote:
> 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?
If none of the D language authors reply in this thread or post to the bug
report, we'll never know what was intended.
More information about the Digitalmars-d
mailing list