Possible?

Ary Borenszweig ary at esperanto.org.ar
Mon Nov 16 04:31:49 PST 2009


Simen Kjaeraas wrote:
> Ary Borenszweig <ary at esperanto.org.ar> wrote:
> 
>> How can I know at compile time if all of the following are true for a 
>> given symbol s:
>>   1. s is a function
>>   2. the return type is a class that extends from class foo.Bar
>>   3. the function has three arguments and it is not variadic
>>   4. the first argument is an int
>>   5. the second argument is a struct
>>   6. the third argument is an associative array with char[] key and 
>> float value
> 
> 
> C f1( int a, S b, float[ char[] ] c ) { return null; }
> D f2( int a, S b, float[ char[] ] c ) { return null; }
> C f3( long a, S b, float[ char[] ] c ) { return null; }
> C f4( int a, string b, float[ char[] ] c ) { return null; }
> C f5( int a, S b, float[ const char[] ] c ) { return null; }
> E f6( int a, string b, float[ char[] ] c ) { return null; }
> E f7( int a, S b, float[ const char[] ]c ) { return null; }
> C f8( T... )( int a, S b, float[ char[] ] c, T args ) { return null; }
> 
> 
> template check( alias f ) {
>     pragma( msg, (&f).stringof[2..$] );
>     pragma( msg, "isFunction: " ~
>         is( typeof( f ) == function ).stringof );
>     pragma( msg, "isvariadic: " ~
>         to!string( ParameterTypeTuple!( f ).length != 3 ) );
>     pragma( msg, "  Derived from C: " ~
>         is( ReturnType!( f ) : C ).stringof );
>     pragma( msg, "  p1 == int: " ~
>         is( Unqual!( ParameterTypeTuple!( f )[0] ) == int ).stringof );
>     pragma( msg, "  p2 == struct: " ~
>         is(ParameterTypeTuple!( f )[1] == struct ).stringof );
>     pragma( msg, "  p2 == float[ char[] ]: " ~
>         is(ParameterTypeTuple!( f )[2] == float[char[]] ).stringof );
> }
> 
> mixin check!( f1 );
> mixin check!( f2 );
> mixin check!( f3 );
> mixin check!( f4 );
> mixin check!( f5 );
> mixin check!( f6 );
> mixin check!( f7 );
> mixin check!( f8!( int ) );

Cool, thanks!

I just wanted to see how you deal in D with all these reflection stuff. 
I think it's very bad that you have to look at "is", "typeof", 
"std.traits" and "std.conv" to accomplish such things (very hard to find 
if you don't ask here) and also that "(&f).stringof[2..$]" is very 
misterious... It would be nice to have a uniform syntax for this.


More information about the Digitalmars-d-learn mailing list