__traits(getMember)

Márcio Martins via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 2 09:30:04 PDT 2016


Can we get a getMember and a getOverloads that won't check for 
visibility or anything else?

__traits appears really powerful, but every-time I try to use it 
for anything other than a toy example or very simple 
serialization, it seems like everything falls apart due to some 
detail... and I end up having to hack it up with string mixins 
and static if(is(typeof(() { some_code; }))).


Also, I found this bug:

import std.datetime;
import std.stdio;
import std.traits;
import std.meta;

template staticIota(int start, int stop, int step = 1) {
   static if(start < stop) {
     alias staticIota = AliasSeq!(start, staticIota!(start + step, 
stop, step));
   } else {
     alias staticIota = AliasSeq!();
   }
}

struct M {
   void test(T)(T x) {
     enum MemberName = "day";
     foreach (i; staticIota!(0, __traits(getOverloads, x, 
MemberName).length)) {
       alias Member = Alias!(__traits(getOverloads, x, 
MemberName)[i]);
       alias MemberType = typeof(&__traits(getOverloads, x, 
MemberName)[i]);

       static if (is(FunctionTypeOf!(Member)) && 
(__traits(getProtection, Member) == "public")) {
         auto addr = &Member; //Error: this for day needs to be 
type Date not type M
         pragma(msg, MemberName, MemberType);
         writeln("addr: ", addr);
       }
     }
   }
}

void main() {
   Date x;
   M m;
   m.test(x);
}

// output:
errocase.d(23): Error: this for day needs to be type Date not 
type M
dayubyte delegate() const pure nothrow @property @safe
errocase.d(23): Error: this for day needs to be type Date not 
type M
dayvoid delegate(int day) pure @property @safe
errocase.d(34): Error: template instance errocase.M.test!(Date) 
error instantiating


It works fine if test(T)() is not a member function...


More information about the Digitalmars-d mailing list