Thread overview
Need help understanding Tuple
Dec 18, 2012
d coder
Dec 18, 2012
bearophile
Dec 18, 2012
d coder
Dec 18, 2012
John Chapman
Dec 18, 2012
d coder
December 18, 2012
Greetings

Somebody please help me understand why we need the Tuple template in the following code. Does not __traits(getAttributes, foo.a) return a tuple? So what is the Tuple template doing here? Converting a tuple to a tuple?

Also where to learn more about tuples in D. Info in TDPL is sketchy.

Regards
- Puneet

template Tuple(T...) {
  alias T Tuple;
}

enum Bar;
class Foo {
  @Bar int a;
}

void main()
{
  Foo foo = new Foo;
  alias Tuple!(__traits(getAttributes, foo.a)) tp;
  pragma(msg, tp);
}


December 18, 2012
d coder:

> template Tuple(T...) {
>   alias T Tuple;
> }
>
> enum Bar;
> class Foo {
>   @Bar int a;
> }
>
> void main()
> {
>   Foo foo = new Foo;
>   alias Tuple!(__traits(getAttributes, foo.a)) tp;
>   pragma(msg, tp);
> }

As first step, don't use that Tuple, use std.typetuple.TypeTuple instead. It's the same, but using the standardized name helps readability.

Bye,
bearophile
December 18, 2012
> As first step, don't use that Tuple, use std.typetuple.TypeTuple instead. It's the same, but using the standardized name helps readability.
>

Thanks for the correction. Tuple is indeed confusing with std.typecons.Tuple which is a struct tuple.

So here is the code again.

enum Bar;
class Foo {
  @Bar int a;
}

void main()
{
  import std.typetuple;
  Foo foo = new Foo;
  alias TypeTuple!(__traits(getAttributes, foo.a)) tp;
  pragma(msg, tp);
}


December 18, 2012
On Tuesday, 18 December 2012 at 11:06:12 UTC, d coder wrote:
> Greetings
>
> Somebody please help me understand why we need the Tuple template in the
> following code. Does not __traits(getAttributes, foo.a) return a tuple? So
> what is the Tuple template doing here? Converting a tuple to a tuple?
>
> Also where to learn more about tuples in D. Info in TDPL is sketchy.

The spec goes into tuples in some depth: http://dlang.org/tuple.html
December 18, 2012
> The spec goes into tuples in some depth: http://dlang.org/tuple.html
>

Thanks John.