Allow Conditional Compilation Inside Enum Declaration

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 3 17:57:31 UTC 2024


On Saturday, 30 March 2024 at 14:57:00 UTC, IchorDev wrote:

> For an enum type with many members—or many conditionals—this 
> quickly becomes an insane amount of repetition.
>
> The logical solution is to just allow conditional compilation 
> statements inside enums:
> ```d
> enum A{
> 	x,y,z,
>     static if(cond){
> 		w,
> 	}
> }
> ```

I don't think static if fits here. There are already special 
cases for enums which allow things that don't make sense 
elsewhere when using a declaration list. In particular, requiring 
the trailing comma is troublesome.

I think what might be in order is to elevate enums to a more 
structured type (like Swift does).

Something like:

```d
enum ??? foo {
    case x;
    case y;
    case z;
    // constructors?
    this(int n) { this._value = cast(foo)n; }
    // operators?
    opBinary(...)
    // static if works at a declaration scope!
    static if(cond) {
      case a;
    }
}
```

The ??? is because I'm assuming it would be too ambiguous with 
existing syntax. Maybe `enum struct`?

But enums were basically copied from C (with duct-taped on 
features), and other modern languages (such as swift) have really 
considered how enums should be designed.

-Steve


More information about the dip.ideas mailing list