www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why TypeTuple can be assigned to a variable

reply "Zhenya" <zheny list.ru> writes:
Hi!

I was just surprised when realized, that this code compiles and 
runs:

import std.typetuple;
import std.stdio;

void main()
{
  auto foo = TypeTuple!("foo","bar");
writeln(typeid(typeof(foo)));
  writeln(foo);
}

If I were compiler expert,I'd say that it's a bug.But I am not)
So, can anybody explain why it's work?

Thank you.
Jun 12 2013
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya <zheny list.ru> wrote:

 Hi!

 I was just surprised when realized, that this code compiles and runs:

 import std.typetuple;
 import std.stdio;

 void main()
 {
   auto foo = TypeTuple!("foo","bar");
 writeln(typeid(typeof(foo)));
   writeln(foo);
 }

 If I were compiler expert,I'd say that it's a bug.But I am not)
 So, can anybody explain why it's work?
It is the equivalent of: TypeTuple!(string, string) foo; foo[0] = "foo"; foo[1] = "bar"; The ability to have a tupetuple as a variable is very useful - if that had not been possible one would need something akin to this: struct Tuple(T...) { T[0] head; static if (T.length) { Tuple!(T[1..$]) tail; } } And to access the third element of a tuple one'd need to write: myTuple.tail.tail.head = "foo"; Clearly this is suboptimal, so D has better ways of doing such things. -- Simen
Jun 12 2013
parent reply "Zhenya" <zheny list.ru> writes:
On Wednesday, 12 June 2013 at 08:14:06 UTC, Simen Kjaeraas wrote:
 On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya <zheny list.ru> 
 wrote:

 Hi!

 I was just surprised when realized, that this code compiles 
 and runs:

 import std.typetuple;
 import std.stdio;

 void main()
 {
  auto foo = TypeTuple!("foo","bar");
 writeln(typeid(typeof(foo)));
  writeln(foo);
 }

 If I were compiler expert,I'd say that it's a bug.But I am not)
 So, can anybody explain why it's work?
It is the equivalent of: TypeTuple!(string, string) foo; foo[0] = "foo"; foo[1] = "bar"; The ability to have a tupetuple as a variable is very useful - if that had not been possible one would need something akin to this: struct Tuple(T...) { T[0] head; static if (T.length) { Tuple!(T[1..$]) tail; } } And to access the third element of a tuple one'd need to write: myTuple.tail.tail.head = "foo"; Clearly this is suboptimal, so D has better ways of doing such things.
OK,you say that TypeTuple!("foo","bar") is a cool value of type TypeTuple!(string,string),right? Then could you tell me what type has [TypeTuple!("foo","bar")]? Currently it is evaluated to string[].And I think if TypeTuple had value semantic, this type would be TypeTuple!(...)[]. This behaviour confuses me a bit. And I just don't understand why do we need TypeTuple's value semantic to implement std.Tuple,because AFAIK it use variadic template parameter pack != TypeTuple. Sorry for my english.
Jun 12 2013
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya <zheny list.ru> wrote:

 OK,you say that TypeTuple!("foo","bar") is a cool value of type
 TypeTuple!(string,string),right?
Well, yes and no, not really. It's a bit magical. In your case, it's assigned to an auto variable, and that variable gets that type. There are other ways to use a TypeTuple where it has other semantics, as you write yourself. As explained below, a TypeTuple is just a bag of template parameters, and thus obeys the rules for a bag of template parameters.
 This behaviour confuses me a bit.
Understandable. It's not entirely straightforward, because the concerns of usability weigh heavier than those of consistency.
 And I just don't understand why do we need TypeTuple's value
 semantic
 to implement std.Tuple,because AFAIK it use variadic template
 parameter pack
    != TypeTuple.
The definition od std.typetuple.TypeTuple is: template TypeTuple(T...){ alias TypeTuple = T; } So a TypeTuple is exactly the same as a variadic template parameter pack.
 Sorry for my english.
No need to be, your English is great. -- Simen
Jun 12 2013
parent "Zhenya" <zheny list.ru> writes:
On Wednesday, 12 June 2013 at 10:09:07 UTC, Simen Kjaeraas wrote:
 On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya <zheny list.ru> 
 wrote:

 OK,you say that TypeTuple!("foo","bar") is a cool value of type
 TypeTuple!(string,string),right?
Well, yes and no, not really. It's a bit magical. In your case, it's assigned to an auto variable, and that variable gets that type. There are other ways to use a TypeTuple where it has other semantics, as you write yourself. As explained below, a TypeTuple is just a bag of template parameters, and thus obeys the rules for a bag of template parameters.
 This behaviour confuses me a bit.
Understandable. It's not entirely straightforward, because the concerns of usability weigh heavier than those of consistency.
 And I just don't understand why do we need TypeTuple's value
 semantic
 to implement std.Tuple,because AFAIK it use variadic template
 parameter pack
   != TypeTuple.
The definition od std.typetuple.TypeTuple is: template TypeTuple(T...){ alias TypeTuple = T; } So a TypeTuple is exactly the same as a variadic template parameter pack.
 Sorry for my english.
No need to be, your English is great.
Thank you for your explaination,I understand.
Jun 12 2013