enums and version/static if/"inheritance"

Nick Treleaven nick at geany.org
Tue Jul 30 19:32:45 UTC 2024


On Monday, 29 July 2024 at 11:25:45 UTC, Nick Treleaven wrote:
> Possible workaround if each enum member is given a specific 
> initializer:
> ```d
> struct FooEnum
> {
>     int A = 5, B = 6;
>     version (x86_64) {
>         int C = 7;
>     } else version (AArch64) {
>         int C = 17;
>     } else {
>         static assert(0);
>     }
> }
>
> mixin enumGen!(FooEnum, "FOO");

Of course, the mixin there is not doing anything more than a 
struct with enum members:
```d
version = x86_64;

struct FOO
{
     enum A = 5, B = 6;
     version (x86_64) {
         enum C = 7;
     } else version (AArch64) {
         enum C = 17;
     } else {
         static assert(0);
     }
}

static assert(FOO.A == 5);
static assert(FOO.B == 6);

version (x86_64)
static assert(FOO.C == 7);
```

But potentially the mixin approach with int members could do more 
- e.g. auto increment any int members without initializer (i.e. 
whose value is zero), based on the previous lexical member 
initializer.


More information about the Digitalmars-d mailing list