www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - __traits(getMember)

reply =?UTF-8?B?TcOhcmNpbw==?= Martins <marcioapm gmail.com> writes:
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...
Nov 02 2016
next sibling parent Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wednesday, November 02, 2016 16:30:04 Márcio Martins via Digitalmars-d 
wrote:
 Can we get a getMember and a getOverloads that won't check for
 visibility or anything else?
That's coming. There was a big discussion on it after the import rules were changed. But IIRC, it was decided that we wouldn't just switch to that immediately (maybe because it was a point release?). I don't remember the details now, so I guess that I'm not very helpful. I'd suggest trying 2.072.0, now that it's out, and seeing if that fixes your problem. If not, 2.073.0 probably will when we get it - which wouldn't be very helpful now, but at least you'd know that the change was coming. - Jonathan M Davis
Nov 02 2016
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2016-11-02 17:30, Márcio Martins wrote:
 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; }))).
For serialization, where you most likely only need to access the fields, you can use .tupleof which will bypass the protection. -- /Jacob Carlborg
Nov 02 2016
parent =?UTF-8?B?TcOhcmNpbw==?= Martins <marcioapm gmail.com> writes:
On Wednesday, 2 November 2016 at 19:49:24 UTC, Jacob Carlborg 
wrote:
 On 2016-11-02 17:30, Márcio Martins wrote:
 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; }))).
For serialization, where you most likely only need to access the fields, you can use .tupleof which will bypass the protection.
Jonathan I am on 2.072.0, but good to know it's coming. Jacob Thanks, that's good to know!
Nov 03 2016