Foreach loops on static arrays error message

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 6 02:11:44 PDT 2017


On Thursday, 6 July 2017 at 08:26:42 UTC, Guillaume Chatelet 
wrote:
> A correct lowering would be:
>
>> ubyte[256] data;
>> for(ubyte i = 0;;++i) {
>>    ubyte x = data[i];
>>    ...
>>    if(i==255) break;
>> }

That could lead to two branches in machine language, try to think 
about it in terms of if and do-while loops to mirror typical 
machine language. The correct lowering is:


ubyte[256] data;

if  (data.length > 0) {
    ubyte i = 0;
    do {
         writeln(i);
     } while ((++i) != cast(ubyte)data.length);
}



More information about the Digitalmars-d mailing list