Retreive method given object, name and arguments

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 13:30:17 PDT 2016


On Thursday, 11 August 2016 at 20:27:51 UTC, Michael Coulombe 
wrote:
> Is there a way to implement "getSymbolOfCall" and 
> "getDelegateOfCall" such that doit is functionally equivalent 
> to calling the method directly?
>
> auto doit(C, string methodName, Args...)(C c, Args args) {
>     alias methodSymbol = getSymbolOfCall!(c, methodName, Args);
>     pragma(msg, hasUDA!(methodSymbol, "my attr"));
>     auto dg = getDelegateOfCall!(c, methodName, Args);
>     return dg(args);
> }
>
> They should deal with getting the right overload, 
> opDispatch-ing, and deducing template arguments from the real 
> argument list to get a concrete delegate pointer. methodSymbol 
> should give access to compile-time introspection like full 
> signature and UDAs.
>
> The ability to do this for non-member functions would be cool 
> too, but is beyond my use case.

Maybe I'm not understanding correctly, but I think you can use 
string mixins:

auto doit(string methodName, C, Args...)(C c, Args args)
{
     mixin("return c." ~ methodName ~ "(args);");
}


More information about the Digitalmars-d-learn mailing list