Ranges, constantly frustrating

bearophile bearophileHUGS at lycos.com
Fri Feb 14 14:37:09 PST 2014


Marc Schütz:

> As far as I understand it, it's about adding an index to 
> _foreach_, as is already supported for arrays:
>
> foreach(v; [1,2,3,4])
>     writeln(v);
> foreach(i, v; [1,2,3,4])
>     writeln(i, " => ", v);
>
> But for ranges, the second form is not possible:
>
> foreach(v; iota(4))           // ok
>     writeln(v);
> foreach(i, v; iota(4))        // Error: cannot infer argument 
> types
>     writeln(i, " => ", v);

I see. In my post I have explained why this is a bad idea (it's 
not explicit so it gives confusion, and it complicates the 
language/compiler).

A better design is to remove the auto-indexing feature for arrays 
too, and use .enumerate in all cases, as in Python.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list