www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template for extracting types with staticMap?

reply Justin Whear <justin economicmodeling.com> writes:
I find this template to be really handy for use with staticMap, and I'm 
wondering if it exists in Phobos in some form so that I can stop 
rewriting it:

private template Extract(string fieldName)
{
	template Extract(T)
	{
		mixin(`alias Extract = T.`~fieldName~`;`);
	}
}

Used like so:
struct Foo(T)
{
    alias Type = T;
}

alias FooTypes = staticMap!(Extract!"Type", TypeTuple!(Foo!int, Foo!
double));
Jul 31 2013
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/1/13, Justin Whear <justin economicmodeling.com> wrote:
 I find this template to be really handy for use with staticMap, and I'm
 wondering if it exists in Phobos in some form so that I can stop
 rewriting it:
Try this: import std.typetuple; template ExtractTypes(T) { static if (is(T : T!Y, Y...)) alias ExtractTypes = Y; else static assert(0); } struct Foo(T) { } void main() { alias FooTypes = staticMap!(ExtractTypes, TypeTuple!(Foo!int, Foo!double)); pragma(msg, FooTypes); }
Jul 31 2013