No index for cycle ranges

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 2 12:23:26 PDT 2011


On Thu, 02 Jun 2011 14:17:26 -0400, Andrej Mitrovic <none at none.none> wrote:

> import std.range;
>
> void main()
> {
>     auto arr = [1, 2, 3, 4, 5, 6, 7, 8];
>     auto foo = cycle(arr);
>    // nope
>     foreach (int index, int val; foo)
>     {
>     }
>    // nope
>     foreach (int index, int val; take(foo, 5))
>     {
>     }
>    // ok
>     foreach (int index, int val; take(arr, 5))
>     {
>     }
> }
>
> Is this because cycle is an infinite range, and index might overflow? I  
> could understand that. But I think if I use take() then there should be  
> no problem with overflows. It still doesn't work though.

As bearophile says, there is no standard for the index portion of a range  
(except for slices, which also happen to be ranges) during foreach.

The reason take(arr, 5) works is because it translates to arr[0..5] which  
is a slice.

-Steve


More information about the Digitalmars-d-learn mailing list