tuples

Jarrett Billingsley jarrett.billingsley at gmail.com
Fri Jun 12 08:00:23 PDT 2009


On Fri, Jun 12, 2009 at 12:10 AM, Ellery
Newcomer<ellery-newcomer at 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);


More information about the Digitalmars-d-learn mailing list