How to list aggregate members in order of declaration at compile time?

Ivan Kazmenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 10 02:16:44 PST 2016


Hi.

I want to somehow list members of a class in the order of their 
declaration.  The immediate goal is to generate a few functions, 
like the "default" constructor for structs but only with all the 
fields, or the "reader" function, but I'm interested in the 
general question as well.

I can go the hard way and wrap every declaration into something 
that will collect them and then list in the right order.  The 
problem is, the declarations won't look normal then.

The documentation for __traits (allMembers, ...), __traits 
(derivedMembers, ...) and the like [1] explicitly says that the 
order is not defined.  Still, I've looked at Atila Neves' 
Cerealed serializer library, and it does use them [2].  Does it 
mean the order is unlikely to change at this point?

The documentation for std.traits' RepresentationTypeTuple [3] 
says things are listed in topological order, which formally does 
not restrict the order for flat structures again.  And the 
underlying implementation [4] of Fields uses .tupleof class 
property which, in turn, does not list any guarantees on the 
order [5].

So I'm confused.  What is considered the right way to list 
members when I care about their linear order?

Ivan Kazmenko.

[1] https://dlang.org/spec/traits.html#derivedMembers
[2] 
https://github.com/atilaneves/cerealed/blob/master/src/cerealed/cereal.d#L467
[3] 
https://dlang.org/phobos/std_traits.html#RepresentationTypeTuple
[4] 
https://github.com/dlang/phobos/blob/10cd84a/std/traits.d#L2279
[5] https://dlang.org/spec/class.html#class_properties



More information about the Digitalmars-d-learn mailing list