Tuple/TypeTuple etc.

Dicebot public at dicebot.lv
Sat Aug 17 05:37:21 PDT 2013


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.

> 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.

> 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.

> 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. And std.typecons.Tuple is completely different 
beast, it simply a template struct.

See example:

import std.typetuple;
import std.typecons;
import std.stdio;

void main()
{
	alias TT = TypeTuple!(int, string);
	enum ET = TypeTuple!(2, "2");
	TT twoVars; twoVars[0] = 2; twoVars[1] = "2";
	writeln(typeid(typeof(twoVars)));
	writeln(twoVars);
	writeln(typeid(typeof(ET)));
	writeln(ET);
	auto different = tuple(2, "2");
	writeln(typeid(typeof(different)));
	writeln(different);
	writeln(__traits(allMembers, typeof(different)));
}

------------------------

(int,immutable(char)[])
22
(int,immutable(char)[])
22
std.typecons.Tuple!(int, string).Tuple
Tuple!(int, string)(2, "2")
parseSpecsFieldSpecfieldSpecsextractTypeextractNameinjectNamedFieldssliceSpecsexpandSpecisCompatibleTuplesTypesexpand_0_1fieldat__ctoropEqualsopCmpopAssign_workaround4424slicelengthtoString_expand_field_0_expand_field_1

http://dpaste.dzfl.pl/b98d8537


More information about the Digitalmars-d mailing list