digitalmars.D.learn - tuples
- Ellery Newcomer (5/5) Jun 11 2009 what is up with this code?
- Saaa (9/14) Jun 11 2009 template Tuple(E...)
- Jarrett Billingsley (17/22) Jun 12 2009 It has to do with the way Tuple is implemented in std.typecons:
- Jarrett Billingsley (4/30) Jun 12 2009 Er,
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)
Jun 11 2009
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)
Jun 11 2009
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);
Jun 12 2009
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:Er, pragma(msg,typeof(t.field[2 .. $]).stringof);what is up with this code? auto t =3D 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: =A0 =A0union =A0 =A0{ =A0 =A0 =A0 =A0Types field; =A0 =A0 =A0 =A0mixin(tupleFields!(0, T)); =A0 =A0} "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: =A0 =A0 =A0 =A0pragma(msg,typeof(t.field[$ - 2 .. $]).stringof);
Jun 12 2009