Associative Array .byKey / .byValue: Counter and Tuples

Q. Schroll via Digitalmars-d digitalmars-d at puremagic.com
Sun Apr 3 03:59:47 PDT 2016


Simple as that, suppose
     uint[uint] aa;
Any range supports carrying an index. Not so does the Range 
returned by byKey and byValue.
     foreach (i, k; aa.byKey) { }
and
     foreach (i, v; aa.byValue) { }
both don't compile.

Reason (I found out by chance):

If the key or value type is a std.typecons.Tuple, iteration over 
aa.by* decomposes the Tuple if there is the right number of 
arguments. For 2-Tuples, there cannot be both possible.

     alias Tup = Tuple!(int, int);
     int[Tup] it;
     Tup[int] ti;

     foreach (x, y; it.byKey) { }
     foreach (x, y; ti.byValue) { }

Why is this undocumented? http://dlang.org/spec/hash-map.html 
doesn't mention Tuples at all!
Why is this useful? Anyone can decompose the Tuple with .expand 
if they like. I would prefer allowing an index.

If it does not meet the spec, is it a bug then?


More information about the Digitalmars-d mailing list