Class Data Members Name Reflection

2014-06-10 Thread Nordlöw
Is there a way to iterate over the symbolic names of the data members of a class instance? I'm currently using .tupleof to get its values (and in turn types) to implement pretty printing to multiple backends (currently testing HTML) using as much of D's compile time reflection as possible:

Re: Class Data Members Name Reflection

2014-06-10 Thread Adam D. Ruppe via Digitalmars-d-learn
Two options: do allMembers and filter it out to only be data members, or do some slicing of tupleof.stringof: S s; foreach(idx, member; s.tupleof) { writeln(Name: , s.tupleof[idx].stringof[2..$]); } The tupleof[idx] inside the loop is important instead of just using member because then

Re: Class Data Members Name Reflection

2014-06-10 Thread Nordlöw
On Tuesday, 10 June 2014 at 16:13:31 UTC, Adam D. Ruppe wrote: Two options: do allMembers and filter it out to only be data members, or do some slicing of tupleof.stringof: What trait should I use to filter out data members? S s; foreach(idx, member; s.tupleof) { writeln(Name: ,

Re: Class Data Members Name Reflection

2014-06-10 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 16:30:52 UTC, Nordlöw wrote: What trait should I use to filter out data members? No trait, more like an is thing to see if the thing has an init. I think static if(is(typeof(__traits(getMember, Thing, name).init)) { } will do it. (BTW the sample chapter of my

Re: Class Data Members Name Reflection

2014-06-10 Thread Dicebot via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 16:10:09 UTC, Nordlöw wrote: Is there a way to iterate over the symbolic names of the data members of a class instance? I'm currently using .tupleof to get its values (and in turn types) to implement pretty printing to multiple backends (currently testing HTML)

Re: Class Data Members Name Reflection

2014-06-10 Thread Nordlöw
I am not sure I understand the question. Does this help? struct A { int x; double y; } void main() { foreach (idx, elem; A.init.tupleof) { pragma(msg, __traits(identifier, A.tupleof[idx])); } } // output: // x // y Exactly what I

Re: Class Data Members Name Reflection

2014-06-10 Thread Nordlöw
BTW: Can DMD serve use file and line location of user defined type aswell? Thx! Correction: I mean serve *us*

Re: Class Data Members Name Reflection

2014-06-10 Thread Nordlöw
On Tuesday, 10 June 2014 at 17:29:35 UTC, Dicebot wrote: On Tuesday, 10 June 2014 at 16:10:09 UTC, Nordlöw wrote: Is there a way to iterate over the symbolic names of the data members of a class instance? I'm currently using .tupleof to get its values (and in turn types) to implement

Re: Class Data Members Name Reflection

2014-06-10 Thread Kapps via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 16:13:31 UTC, Adam D. Ruppe wrote: Two options: do allMembers and filter it out to only be data members, or do some slicing of tupleof.stringof: S s; foreach(idx, member; s.tupleof) { writeln(Name: , s.tupleof[idx].stringof[2..$]); } The tupleof[idx] inside the

Re: Class Data Members Name Reflection

2014-06-10 Thread Kapps via Digitalmars-d-learn
On Tuesday, 10 June 2014 at 20:01:37 UTC, Nordlöw wrote: Notice that A.init.tupleof segfaults for classes so that is _not_ an adviced solution in a generic solution! There's no need to use .init: import std.stdio; struct Foo { int a, b; } void main() {