Unexpected behaviour of foreach statement
Arredondo
arm.plus at gmail.com
Fri Mar 2 10:46:02 UTC 2018
On Friday, 2 March 2018 at 10:32:08 UTC, Jonathan M Davis wrote:
> foreach does not support indices for ranges, only arrays. When
> you have
>
> foreach(e; range)
>
> it gets lowered to
>
> foreach(auto __range = range; !__range.empty;
> __range.popFront())
> {
> auto e = __range.front;
> }
>
> There are no indices involved there, and if a range isn't a
> random-access range, it doesn't support any kind of indices
> anyway. The compiler would have to add a variable to count the
> elements, and it doesn't support that.
I understand. I guess I was expecting the compiler to
automatically do something along the lines of what enumerate
does. Although, a nicer error message would have saved the day
just as well.
Arredondo
More information about the Digitalmars-d-learn
mailing list