Why TypeTuple can be assigned to a variable
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.
Re: Why TypeTuple can be assigned to a variable
On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya zh...@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
Re: Why TypeTuple can be assigned to a variable
On Wednesday, 12 June 2013 at 08:14:06 UTC, Simen Kjaeraas wrote: On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya zh...@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.
Re: Why TypeTuple can be assigned to a variable
On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya zh...@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
Re: Why TypeTuple can be assigned to a variable
On Wednesday, 12 June 2013 at 10:09:07 UTC, Simen Kjaeraas wrote: On Wed, 12 Jun 2013 11:44:19 +0200, Zhenya zh...@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.