Define an order for allMembers traits
Paul Backus
snarwin at gmail.com
Wed Aug 23 15:32:02 UTC 2023
On Wednesday, 23 August 2023 at 12:32:58 UTC, Andrey Zherikov
wrote:
> This will especially help in my use case where I need to know
> the order in which struct members are declared. Right now I'm
> using UDA with index to explicitly set an order:
> ```d
> struct S
> {
> @UDA(1) int a;
> @UDA(2) int b;
> @UDA(3) string c;
> @UDA(4) string d;
> }
> ```
For this use case, you should use `.tupleof` instead of
`__traits(allMembers)`, which [is guaranteed by the spec to give
the fields in declaration order][1].
[1]: https://dlang.org/spec/class.html#class_properties
More information about the Digitalmars-d
mailing list