www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - why MemberFunctionsTuple not support struct ?

reply Newbie2019 <newbie2019 gmail.com> writes:
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);
Jul 30 2019
parent reply CommanderZot <no no.no> writes:
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);
Jul 30 2019
parent Newbie2019 <newbie2019 gmail.com> writes:
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; } }
Jul 30 2019