Iterating over the tupleof of a struct
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Aug 23 13:47:31 PDT 2014
On Saturday, 23 August 2014 at 20:34:35 UTC, Ali Çehreli wrote:
> There are a number of inconsistencies around tuples. The
> behavior you expect is present for ranges that return tuple
> fronts:
>
> import std.stdio;
> import std.typecons;
> import std.range;
>
> void main()
> {
> auto t = [ tuple(1.5, 100), tuple(2.5, 200) ];
>
> foreach (a, b; t.retro) {
> writefln("%s, %s", a, b);
> }
> }
>
> Because t.retro is a range, the foreach extracts the members of
> the tuple and we get the folloing output:
>
> 2.5, 200
> 1.5, 100
>
> Now, remove the .retro part; the range becomes a slice, in
> which case 'a' becomes the iteration counter and 'b' becomes
> the tuple value:
>
> 0, Tuple!(double, int)(1.5, 100)
> 1, Tuple!(double, int)(2.5, 200)
>
> Is that a WAT? :)
>
> Ali
I cannot wait until we get proper tuple destructuring and this
buggy foreach unpacking dies.
More information about the Digitalmars-d-learn
mailing list