On Friday, 14 December 2012 at 20:14:29 UTC, Philippe Sigaud wrote:
On Fri, Dec 14, 2012 at 1:18 AM, bearophile <[email protected]>wrote:


Fit to be added to Phobos?


Maybe, I don't know. People seem to ask for this quite regularly.

Here is a slightly improved version, what do you think?

template isTemplatedType(Type...) if (Type.length == 1)
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        enum isTemplatedType = true;
    else
        enum isTemplatedType = false;
}

template TemplateArity(Type...) if (Type.length == 1 &&
isTemplatedType!(Type[0]))
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        enum TemplateArity = Args.length;
}

template TemplateArgs(Type...) if (Type.length == 1 &&
isTemplatedType!(Type[0]))
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        alias Args TemplateArgs;
}

void main()
{
    alias Tuple!(int, double) T;
    writeln(TemplateArity!T);
    writeln(TemplateArgs!T.stringof);
    writeln(isTemplatedType!T);
}

Oops ;/ How do I get the number of type arguments if I don't know them yet? I know this sort of seems wrong but somehow, to know how to alias the class I need to know it's number of arguments. (I don't think it makes too much sense with the type system but not sure how to solve the following problem without it:

http://forum.dlang.org/thread/[email protected]#post-yhhtuojlsgiykyrfhngl:40forum.dlang.org)

Reply via email to