www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Process a TypeTuple

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
Is it possible to apply some operation on every member of a 
TypeTuple, then get the result back?

Say I have a TypeTuple of array types, and I want a TypeTuple of 
their element types, how could I do that?
Jun 14 2015
parent reply "Baz" <bb.temp gmx.com> writes:
On Monday, 15 June 2015 at 03:53:35 UTC, Yuxuan Shui wrote:
 Is it possible to apply some operation on every member of a 
 TypeTuple, then get the result back?

 Say I have a TypeTuple of array types, and I want a TypeTuple 
 of their element types, how could I do that?
You can do that with std.typetuple.staticMap, example: --- import std.stdio; import std.typetuple; template ElemType(T) { alias ElemType = typeof(T.init[0]); } void main(string[] args) { alias T1 = TypeTuple!(int[],ubyte[]); alias T2 = staticMap!(ElemType, T1); writeln(typeof(T1.init).stringof); writeln(typeof(T2.init).stringof); } --- outputs: --- (int[], ubyte[]) (int, ubyte) --- It's like the higher-order function map() but for type list: http://dlang.org/phobos/std_typetuple.html#.staticMap
Jun 14 2015
parent Justin Whear <justin economicmodeling.com> writes:
On Mon, 15 Jun 2015 04:06:12 +0000, Baz wrote:

 On Monday, 15 June 2015 at 03:53:35 UTC, Yuxuan Shui wrote:
 Is it possible to apply some operation on every member of a TypeTuple,
 then get the result back?

 Say I have a TypeTuple of array types, and I want a TypeTuple of their
 element types, how could I do that?
For this particular example you use std.range.ElementType: import std.typecons, std.range; alias Elements = staticMap!(ElementType, MyRangeOrArrayTypes);
Jun 15 2015