enums and version/static if/"inheritance"

Walter Bright newshound2 at digitalmars.com
Thu Aug 1 17:55:04 UTC 2024


On 7/31/2024 7:06 PM, IchorDev wrote:
> You said that it hurts your head to read code like that. But even if it’s 
> ‘fine’, it would compile faster & take fewer lines

Not perceptibly faster (the lexer is very fast), and fewer lines is not always 
the best thing.

> if we could put conditional 
> compilation in comma-separated lists, particularly enums and (associative) array 
> literals. I would only need to write one array literal instead of a CTFE lambda 
> with a series of ifs. Notice also that I did not nest those ifs, which is 
> slightly worse for performance, but I had to preserve readability. With a single 
> array literal, I would not have to make these compromises.

I do things like this:

```
/// Map to unsigned version of type
__gshared tym_t[256] tytouns = tytouns_init;
extern (D) private enum tytouns_init =
() {
     tym_t[256] tab;
     foreach (ty; 0 .. TYMAX)
     {
         tym_t tym;
         switch (ty)
         {
             case TYchar:      tym = TYuchar;    break;
             case TYschar:     tym = TYuchar;    break;
             case TYshort:     tym = TYushort;   break;
             case TYushort:    tym = TYushort;   break;

             case TYenum:      tym = TYuint;     break;
             case TYint:       tym = TYuint;     break;

             case TYlong:      tym = TYulong;    break;
             case TYllong:     tym = TYullong;   break;
             case TYcent:      tym = TYucent;    break;

             case TYschar16:   tym = TYuchar16;  break;
             case TYshort8:    tym = TYushort8;  break;
             case TYlong4:     tym = TYulong4;   break;
             case TYllong2:    tym = TYullong2;  break;

             case TYschar32:   tym = TYuchar32;  break;
             case TYshort16:   tym = TYushort16; break;
             case TYlong8:     tym = TYulong8;   break;
             case TYllong4:    tym = TYullong4;  break;

             case TYschar64:   tym = TYuchar64;  break;
             case TYshort32:   tym = TYushort32; break;
             case TYlong16:    tym = TYulong16;  break;
             case TYllong8:    tym = TYullong8;  break;

             default:          tym = ty;         break;
         }
         tab[ty] = tym;
     }
     return tab;
} ();
```
to make complex initializers. Works great!

I don't know the details of your particular solution.


More information about the Digitalmars-d mailing list