Repost: make foreach(i, a; range) "just work"
Jesse Phillips
Jesse.K.Phillips+D at gmail.com
Fri Feb 21 11:42:41 PST 2014
On Friday, 21 February 2014 at 16:41:00 UTC, Regan Heath wrote:
> and make this possible too:
>
> foreach([index, ]value; range) { }
I understand the user interface is simple, but you created 3
statements about how it could be achieved and work/not work with
the existing setup. Each have their positives and negatives, it
would not make sense to "just choose one" and hope it all works
out.
>> if AA is changed to a double[string], then your value loop
>> iterates on keys and your key loop iterates on values.
>
> No, I was actually suggesting a change here, the compiler would
> use type matching not ordering to assign the variables. So
> because 'v' is a string, it is bound to the value not the key.
And string is the key, double[string] is not the same as
string[double].
Also string[string], ambiguous yet common.
There are many things to consider when adding a feature, it is
not good to ignore what can go wrong.
> Thanks! Ok, so how is this working? ahh, ok I think I get it.
> enumerate returns a range, whose values are Tuples of
> index/value where value is also a tuple so is flattened, and
> then the whole lot is flattened into the foreach.
Sounds like you understand it, seams foreach will flatten all
tuples.
> I don't think this affects what I actually want to change, we
> can have:
>
> foreach(index, value; range) { }
>
> and still flatten tuples into value, you would simply have to
> provide one extra variable to get an index.
>
> Make sense?
Yes, but I'm saying we don't need it because
foreach(index, value; range.enumerate) { }
is good enough. Not perfect, but good enough.
More information about the Digitalmars-d
mailing list