Class Data Members Name Reflection

Dicebot via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jun 10 10:29:34 PDT 2014


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


More information about the Digitalmars-d-learn mailing list