Joining AliasSeq/TypeTuple s

Adrian Matoga via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 29 03:06:43 PDT 2016


On Tuesday, 29 March 2016 at 09:33:40 UTC, Voitech wrote:
> Hi, i want to join two or more tupples in to one, with mixing 
> the indexes like roundRobin but in compile time.
>
> unittest{
> import std.meta;
> alias first=AliasSeq!(int, string,bool);
> alias second=AliasSeq!("abc","def","ghi");
> alias third=...
>
> static assert(third==AliasSeq!(int, "abc", string, "def", bool, 
> "ghi"));
> }
>
>
> How to obtain this kind of functionality ? Is there maybe some 
> builin template for this in phobos ? I couldn't find this.
>
> Thank you
> Voitech.

import std.meta : AliasSeq;

template RR(A...)
	if (!(A.length & 1))
{
	static if (A.length == 0)
		alias RR = AliasSeq!();
	else {
		alias Left = A[0 .. $ / 2];
		alias Right = A[$ / 2 .. $];
		alias RR = AliasSeq!(Left[0], Right[0], RR!(Left[1 .. $], 
Right[1 .. $]));
	}
}

struct S(A...) {} // needed to reliably compare AliasSeq's for 
equality

unittest {
	alias first = AliasSeq!(int, string, bool);
	alias second = AliasSeq!("abc", "def", "ghi");
	alias third = RR!(first, second);
	static assert(is(S!third == S!(int, "abc", string, "def", bool, 
"ghi")));
}



More information about the Digitalmars-d-learn mailing list