why MemberFunctionsTuple not support struct ?

CommanderZot no at no.no
Tue Jul 30 14:20:41 UTC 2019


On Tuesday, 30 July 2019 at 11:33:07 UTC, Newbie2019 wrote:
>
> I need to get the struct method ( function member)  address on 
> reflection,  do some search and find MemberFunctionsTuple, but 
> it only for class.
>
> I think D need a traits for get struct method delegate address.
>  I see there is no nice workaround.
>
> like:  __traits(allMethods, C),   __traits(getMethod, C, name)
>
> struct method delegate address is a function pointer const, so 
> can be use on compile time like function address.
>
> The problem is I can't get it on runtime easy,  D will think I 
> try call the method and return the method return type.
>
> alias KillCallback = scope bool delegate();
>
> struct S {
>     bool kill() {
>      return true;
>    }
> }
>
> pragma(msg, typeof(__traits(getMember, S, "kill")) ); // bool()
> pragma(msg, typeof(KillCallback.init.funcptr)); // bool 
> function()
> enum IS_SAME = __traits(isSame,   typeof(__traits(getMember, S, 
> "kill")), typeof(KillCallback.init.funcptr) );
>
> static assert(IS_SAME);

Is this what you are looking for?:

alias KillCallback = scope bool delegate();

struct S {
     bool kill() {
      return true;
    }
}

pragma(msg, typeof(&__traits(getMember, S, "kill")) ); // bool()
pragma(msg, typeof(KillCallback.init.funcptr)); // bool function()
enum IS_SAME = __traits(isSame,   typeof(&__traits(getMember, S, 
"kill")), typeof(KillCallback.init.funcptr) );

static assert(IS_SAME);


More information about the Digitalmars-d mailing list