Another foreach idea.

Dave Dave_member at pathlink.com
Wed Jun 7 20:41:18 PDT 2006


Instead of:

for(int i = 1_000; i < 1_000_000; i++) {}

How about:

foreach(idx; 1_000 .. 1_000_000)
{
     // 1_000 to 999_999 or 1_000 to 1_000_000?
     // type inferred 'idx' would be an int or long
     //  depending on 32 bit or 64 bit hardware
}

or simply:

size_t x = 1_000_000;

foreach(idx; x)
{
     // 0 to 999_999 or 1 to 1_000_000?
     // type inferred 'idx' is size_t
}

The rational would be not only for brevity but also the reasons given 
here: http://digitalmars.com/d/faq.html#foreach

I can see the problems already - 0 or 1 based? 0 based to be consistent 
with how many for loops are written now and with array indexing (even 
though presumably you'd then use foreach with the array itself)? Or 1 
based because then it would be WYSIWYG.

- Dave



More information about the Digitalmars-d mailing list