Testing if a function is defined in a module

mario pernici mario.pernici at mi.infn.it
Wed Feb 21 08:41:40 PST 2007


Carlos Smith Wrote:

> 
> "Kirk McDonald" <kirklin.mcdonald at gmail.com> wrote 
> 
> : There are a few ways of phrasing this, but here's one:
> : 
> : static if (is(typeof(&YYUSERINIT))) {
> :     // Do stuff with the function
> : }
> 
> Just tried with dmd 1.007, in a program
> defining int a=0;
> 
>   static if( is(typeof(main) == function ) )
>     ...  
>   static if( is(typeof(yymain) == function ) )
>     ...
>   static if( is(typeof(a) == int ) )
>     ...
>   static if( is(typeof(a) == float ) )
>     ...
> 
> All 4 tests gives what they suggest.
> Ie: main is a function, yymain is not.
> a is an int and not a float.
> 
> Great !
> 
> Looks like D wont miss the C preprocessor...
> 


Nice. I Tried to install dmd 1.007 for Linux, but the link is still for 1.006.

One can check the return type of the function with
 static if(is(typeof(YYUSERINIT))) {
    if( typeid(typeof(&YYUSERINIT)).toString() == "int()*")
      writefln("YYUSERINIT is a int()*");
  }

but it works independently of the arguments of the function, 
int YYUSERINIT()
or
int YYUSERINIT(int a, int b)




More information about the Digitalmars-d-learn mailing list