Why is there no range iteration with index by the language?

Q. Schroll qs.il.paperinik at gmail.com
Tue Jun 9 23:53:16 UTC 2020


Is there any particular reason why std.range : enumerate is a 
thing and

     foreach (i, e; range) { ... }

doesn't work from the get-go? I wouldn't have such an issue with 
it if static foreach would work with enumerate just fine. As far 
as I can tell,

     foreach (e; range) { ... }

is being lowered to

     for (auto _range = range; !_range.empty; _range.popFront)
     {
         auto e = _range.front;
         ...
     }

So why cant DMD rewrite

     foreach (i, e; range) { ... }

to

     for (auto _range = range, index = size_t(0); !_range.empty; 
_range.popFront, ++index)
     {
         size_t i = index;
         auto e = _range.front;
         ...
     }

Doesn't seem like a big deal, does it? I'm asking because I 
suspect there's an odd reason I have no idea and I whish to be 
educated.


More information about the Digitalmars-d-learn mailing list