Closures and loop scope

Timon Gehr timon.gehr at gmx.ch
Tue Jun 4 15:24:17 PDT 2013


On 06/05/2013 12:17 AM, Nick Sabalausky wrote:
> On Tue, 04 Jun 2013 23:48:32 +0200
> Timon Gehr <timon.gehr at gmx.ch> wrote:
>>
>> Behind the scenes, the foreach loop is rewritten to something like:
>>
>> for(int __a=0;__a<5;__a++){
>>       int a = __a;
>>       writeln(&a);
>> }
>
> Yea, I think the years I spent using C has corrupted my brain into
> seeing...
>
>     foreach(int a; 0..5)
>
> ...as shorthand for:
>
>     for(int a=0; a < 5; a++) { /* use a */ }
>
> Which is something I wrote far too many times in the 90's ;)
>

It was exactly that until recently. With the language version 
implemented by DMD 2.062, the behaviour in the OP was actually correct.


More information about the Digitalmars-d mailing list