Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 12, 2009 tuples | ||||
---|---|---|---|---|
| ||||
what is up with this code? auto t = tuple(1,'a',3.333,false); pragma(msg,typeof(t.tupleof[2 .. $]).stringof); spits out (double, bool, int, char, double, bool) |
June 12, 2009 Re: tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | > what is up with this code? > > auto t = tuple(1,'a',3.333,false); > pragma(msg,typeof(t.tupleof[2 .. $]).stringof); > > spits out > > (double, bool, int, char, double, bool) template Tuple(E...) { alias E Tuple; } alias Tuple!(1,'a',3.333,false) t; pragma(msg,typeof(t.tupleof[2 .. $]).stringof); //Error pragma(msg,typeof(t[2..$].stringof); //writes (double, bool) |
June 12, 2009 Re: tuples | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ellery Newcomer | On Fri, Jun 12, 2009 at 12:10 AM, Ellery Newcomer<ellery-newcomer@utulsa.edu> wrote: > what is up with this code? > > auto t = tuple(1,'a',3.333,false); > pragma(msg,typeof(t.tupleof[2 .. $]).stringof); > > spits out > > (double, bool, int, char, double, bool) > It has to do with the way Tuple is implemented in std.typecons: union { Types field; mixin(tupleFields!(0, T)); } "field" is of type (double, bool, int, char), and the mixin defines something like "struct { double _0; bool _1; int _2; char _3; }". Since it's a union, the field member overlaps with the anonymous struct, just giving you two different ways of accessing the same member: t.field[0] or t._0. When DMD does the tupleof, it puts all the union members in the tuple, giving you what looks like a duplicated type list. You can instead use: pragma(msg,typeof(t.field[$ - 2 .. $]).stringof); |
June 12, 2009 Re: tuples | ||||
---|---|---|---|---|
| ||||
On Fri, Jun 12, 2009 at 11:00 AM, Jarrett Billingsley<jarrett.billingsley@gmail.com> wrote: > On Fri, Jun 12, 2009 at 12:10 AM, Ellery Newcomer<ellery-newcomer@utulsa.edu> wrote: >> what is up with this code? >> >> auto t = tuple(1,'a',3.333,false); >> pragma(msg,typeof(t.tupleof[2 .. $]).stringof); >> >> spits out >> >> (double, bool, int, char, double, bool) >> > > It has to do with the way Tuple is implemented in std.typecons: > > union > { > Types field; > mixin(tupleFields!(0, T)); > } > > "field" is of type (double, bool, int, char), and the mixin defines something like "struct { double _0; bool _1; int _2; char _3; }". Since it's a union, the field member overlaps with the anonymous struct, just giving you two different ways of accessing the same member: t.field[0] or t._0. > > When DMD does the tupleof, it puts all the union members in the tuple, giving you what looks like a duplicated type list. > > You can instead use: > > pragma(msg,typeof(t.field[$ - 2 .. $]).stringof); Er, pragma(msg,typeof(t.field[2 .. $]).stringof); |
Copyright © 1999-2021 by the D Language Foundation