Top 5

bearophile bearophileHUGS at lycos.com
Tue Oct 28 04:12:31 PDT 2008


Max Samukha:
> Are there still plans for static foreach?

While you/we wait for the static foreach, you can often do something similar defining a Range like this:

template Range(int stop) {
    static if (stop <= 0)
        alias Tuple!() Range;
    else
        alias Tuple!(Range!(stop-1), stop-1) Range;
}

In my libs you can find a Range!() for 2 and 3 arguments too (stop, start-stop, start-stop-stride).

Then you can use it for example like this:

auto const foo = [x1, x2, x3];
foreach (i; Range!(foo.length))
    func(x1);

That foreach is static. But I suggest you to not use it with very large number of iterations.
Bye,
bearophile



More information about the Digitalmars-d mailing list