Tuple/TypeTuple etc.

John Colvin john.loughran.colvin at gmail.com
Sat Aug 17 06:55:08 PDT 2013


On Saturday, 17 August 2013 at 12:37:23 UTC, Dicebot wrote:
> On Saturday, 17 August 2013 at 11:43:09 UTC, John Colvin wrote:
>> On Saturday, 17 August 2013 at 01:01:10 UTC, Dicebot wrote:
>>> Currently it is not the case. typeof(tuple(1,2)) is 
>>> Tuple!(int,int), not TypeTuple!(int, int). But 
>>> typeof(TypeTuple(1,2)) is TypeTyple!(int, int) :)
>>
>> Whu?
>>
>> There is no such type as TypeTuple!(int, int), it is 
>> completely aliased away to a builtin tuple.
>
> Yeah but there is no syntax for them so I am using TypeTuple 
> for unambiguity. pragma(msg) will print (int, int) for it.

ok, fair enough.

>> Also, printing out types is actually rather misleading, as it 
>> chooses when to use or not use tuple() in annoying ways e.g. 
>> TypeTuple!(int, string) is (int, string) but TypeTuple!(int, 
>> 3) is tuple(int, 3)   Either they are both tuples, or neither 
>> is.
>
> They are two types of built-in tuples - pure type tuples and 
> expression/mixed tuples. typeof expression tuple is type tuple. 
> Those have same syntax but slightly different semantics.

Not quite, there are pure type-tuples (can be used as a type), 
pure expression-tuples (can be assigned to) and mixed tuples. But 
then there's also aliases...
	int a;
	auto b = TypeTuple!(a);
	b[0] = 3;
	assert(a == 0);  //passes
clearly b is an instantiation of a typetuple (or an 
expression-tuple, as you point out), but what is actually going 
on in it's initialisation?

>> To the best of my knowledge, there is only this:
>>
>> 1) collections of items, called tuples. They can contain 
>> pretty much anything you want, as long it's available at 
>> compile-time.
>> They are ***not types***. If and only if a tuple contains only 
>> types, the tuple itself is a type. This is often referred to 
>> as a type-tuple. In current D, tuples of all varieties are 
>> created by variadic template parameters.*
>
> All true but last statement. They are not "created" anywhere, 
> this is simply most convenient way to capture built-in tuple in 
> a form usable by user code.

That's what i mean by created. I deliberated over what word to 
use there for quite a while, but perhaps "capture" is better. 
Either way, you've got some ct stuff and you want to group it 
together in to a tuple, variadic template parameters is the way 
to do it.

>> 2) instantiations of type-tuples. These are a collection of 
>> runtime values, individually accessible by indexing or foreach 
>> iteration. std.typecons.Tuple is a wrapper around one of these 
>> providing some extra functionality.
>
> As far as I can observe, instantiations of type tuples are 
> expression tuples.

That's an interesting way of looking at it. I didn't realise one 
could do this:
	auto ET = TypeTuple!(2, "2");
	ET[0] = 5;   //perfectly fine

> And std.typecons.Tuple is completely different beast, it simply 
> a template struct.

Yes, as I said: It's a wrapper around an instantiation of a 
type-tuple.



Every time I think I understand this, there's more...


More information about the Digitalmars-d mailing list