Determining whether a class has a method

Christopher Wright dhasenan at gmail.com
Thu Nov 29 17:17:13 PST 2007


Bill Baxter wrote:
> I think you may be able to use an 'is' check + string mixin if the name 
> string is a compile time constant.
> like
>    static if(is( T.name == function ))
> 
> then mixin-ify that like
> 
> bool has_method(T, string name) {
>    mixin("static if(is T."~name~" == function)) "
>          "{ return true; } else {return false; }");
> }
> 
> 
> --bb

Very nice! It works for every situation but the one I need:

static if (is (T._ctor == function)) {
    // This never happens.
}

If there's no constructor defined for a class or any of its base 
classes, then ParameterTypeTuple!(T._ctor) fails, of course.

I guess I can work around this by requiring an explicit constructor, but 
I don't love it, especially since I can't provide a helpful error 
message in the failing case.


More information about the Digitalmars-d-learn mailing list