enums and version/static if/"inheritance"

Walter Bright newshound2 at digitalmars.com
Wed Jul 31 06:11:15 UTC 2024


This comes up from time to time.

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

I've seen this forever in C. It just makes my brain bleed. Here's the D way:

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

Ah, doesn't that look nicer? It's nicer because the code forms a regular 
pattern. A regular pattern is easier to understand. It also uncovers errors, 
like the `version E = 9,` error in the opening example.

Also, when I'm debugging AArch64 code, I don't want to see x86_64 code 
interleaved in with it. Such makes it too easy to inadvertently mix them up.


More information about the Digitalmars-d mailing list