Define an order for allMembers traits

Stefan Koch uplink.coder at googlemail.com
Wed Aug 23 14:24:46 UTC 2023


On Wednesday, 23 August 2023 at 13:53:30 UTC, Andrey Zherikov 
wrote:
> On Wednesday, 23 August 2023 at 12:39:09 UTC, Adam D Ruppe 
> wrote:
>> On Wednesday, 23 August 2023 at 12:32:58 UTC, Andrey Zherikov 
>> wrote:
>>> Is it possible to define an order at least for some cases 
>>> (I'm looking for structs actually)?
>>
>> What do you do with the order of declaration?
>
> I'm developing a [library to parse command line 
> arguments](https://github.com/andrey-zherikov/argparse). One 
> type of arguments is positional argument which means that they 
> have a position in command line.
> As of now (because the order of `allMembers` is not defined), 
> these arguments are attributed the following way by explicitly 
> providing the order:
> ```d
> struct Params
> {
>     @PositionalArgument(0)
>     string firstName;
>
>     @PositionalArgument(1)
>     string middleName;
>
>     @PositionalArgument(2)
>     string lastName;
> }
> ```
> If `allMembers` guarantees the order to be the same as the 
> members are declared then the code above can be simplified to 
> this:
> ```d
> struct Params
> {
>     @PositionalArgument
>     string firstName, middleName, lastName;
> }
> ```
My 10 cents on this are:

In the general case we cannot guarantee the order of 
__traits(allMembers) as they might be generated by 
meta-programming which does not have an obvious execution order.

So the question is we guarantee lexical order in the cases where 
no meta programming is used to introduce members?
I think we can do that.


More information about the Digitalmars-d mailing list