www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need help understanding Tuple

reply d coder <dlang.coder gmail.com> writes:
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);
}
Dec 18 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 18 2012
parent d coder <dlang.coder gmail.com> writes:
 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); }
Dec 18 2012
prev sibling parent reply "John Chapman" <johnch_atms hotmail.com> writes:
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
Dec 18 2012
parent d coder <dlang.coder gmail.com> writes:
 The spec goes into tuples in some depth: http://dlang.org/tuple.html
Thanks John.
Dec 18 2012