Why TypeTuple can be assigned to a variable
Simen Kjaeraas
simen.kjaras at gmail.com
Wed Jun 12 01:13:51 PDT 2013
On Wed, 12 Jun 2013 10:01:59 +0200, Zhenya <zheny at 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
More information about the Digitalmars-d-learn
mailing list