Thread overview
why MemberFunctionsTuple not support struct ?
Jul 30, 2019
Newbie2019
Jul 30, 2019
CommanderZot
Jul 30, 2019
Newbie2019
July 30, 2019
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);









July 30, 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);
July 30, 2019
On Tuesday, 30 July 2019 at 14:20:41 UTC, CommanderZot wrote:
> 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);

Thanks for the tips.

It can not exclude static function, so I replace it with typeof(&__traits(getMember, S.init, "kill"));

Still, if there is multi kill methods,  then I need a array to handle them.

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