Get class type parameters at compile time

js.mdnq js_adddot+mdng at gmail.com
Fri Dec 14 22:57:45 PST 2012


On Friday, 14 December 2012 at 20:14:29 UTC, Philippe Sigaud 
wrote:
> On Fri, Dec 14, 2012 at 1:18 AM, bearophile 
> <bearophileHUGS at lycos.com>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/wacupbnlqgrfhaebdssd@forum.dlang.org#post-yhhtuojlsgiykyrfhngl:40forum.dlang.org)


More information about the Digitalmars-d-learn mailing list