Define an order for allMembers traits

John Colvin john.loughran.colvin at gmail.com
Thu Aug 24 00:11:07 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;
> }
> ```

Just sort allMembers by .offsetof? You might need a small amount 
of compile time gymnastics but it’s doable.


More information about the Digitalmars-d mailing list