How to assert a function signature in D2.008?

Christopher Wright dhasenan at gmail.com
Tue Dec 4 08:01:33 PST 2007


Janice Caron wrote:
> This used to compile under D2.007
> 
>     struct A
>     {
>         void f() {}
> 
>         static assert(is(typeof(f)==void function()));
>     }
> 
> It doesn't under D2.008. The only way I've found to make it compile
> under D2.008 is to change it to
> 
>     struct A
>     {
>         void f() {}
> 
>         static assert(is(typeof(f)==function));
>     }
> 
> which means I am no longer able to check the function signature. So,
> the question is, /how/ do I check the function signature in D2.008? I
> know I could use ReturnType! and ParameterTypeTuple!, but that would
> be so tedious for functions with more complex signatures. For example:
> 
>     struct A
>     {
>         int f(int,char,float,int) {}
> 
>         static assert(is(typeof(f)==function));
>         static assert(is(ReturnType!(f) == int));
>         static assert(ParameterTypeTuple!(f).length == 4);
>         static assert(is(ParameterTypeTuple!(f)[0] == int));
>         static assert(is(ParameterTypeTuple!(f)[1] == char));
>         static assert(is(ParameterTypeTuple!(f)[2] == float));
>         static assert(is(ParameterTypeTuple!(f)[3] == int));
>     }
> 
> Is this really the "right" way to do things now? The old (D2.007) way
> let me make that check in a single line.

static assert (is (typeof(&f) == typeof(delegate void (int, char, float, 
int){})));

I don't know how to write the type of a delegate so that it can be used 
in an is expression.



More information about the Digitalmars-d mailing list