Class Data Members Name Reflection
"Nordlöw" via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 10 13:01:36 PDT 2014
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 pretty printing to multiple backends
>> (currently testing HTML) using as much of D's compile time
>> reflection as possible:
>>
>> https://github.com/nordlow/justd/blob/master/pprint.d
>>
>> I've currently defined mappings from InputRanges of Aggregates
>> (tuples, structs, and classes) to HTML tables where
>>
>> - the aggregate members are mapped to table columns (and their
>> types in turn two column headers) and
>> - range elements to rows
>>
>> and I would like to perfect the output by also propagating the
>> member names to the column headings.
>>
>> I'm aware of __traits(allMembers, Type) but those return more
>> than .tupleof does.
>>
>> Help please.
>
> 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
Notice that A.init.tupleof segfaults for classes so that is _not_
an adviced solution in a generic solution!
But fortunately I always have an instance, say a, of A available
in the pprint functions so I just use a.tupleof instead! Now it
works!
I'll update pprint.d later today! I'll further extract away the
dumb dependencies for pprint.d in the upcoming days so it can be
reused later on.
Thanks you all!
More information about the Digitalmars-d-learn
mailing list