Allow Conditional Compilation Inside Enum Declaration

Dukc ajieskola at gmail.com
Wed Apr 3 18:47:09 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,
> 	}
> }
> ```

It would be strange if this was enabled just for `enum`s but not 
for other listings. Other possibilities that come to mind:

```D
auto fun(int a, float b, static if(cond){char c, bool d,}) => 
//...

import std.algorithm, std.range, static if(cond){std.conv,};

pragma(msg, "hello", static if(cond){"world!",});
```

Also I'm not quite sure of the syntax. When you list something 
with commas, it resembles an expression, since function argument 
lists and array literals are also listed with commas and 
expressions. But in D, you can't use `if` or `static if` as an 
expression - for that you have the conditional operator `cond ? 
whenTrue : otherwise`. Could this idea would work better with the 
conditional operator syntax?

```D
enum A{x, y, z, cond? w: ()}
```

On second thought, maybe not. You can't do this inside an array 
literal either unless you also have a value for the "else" case, 
and you also can't list multiple values for one condition except 
by defining another array literal outside the original one and 
concatenating them. Maybe it's time to let us use `if`s and 
conditional compilation directives as parts of expressions, so 
it'd be consistent with your proposal?

There's another possibility for enums specifically. This could 
also be solved if you could define them with `;` separators like 
you define classes, structs and unions. If you could define an 
enum by defining an `union` with no non-zero members it'd solve 
this one and also give the [existing sum type 
proposals](https://forum.dlang.org/post/zgsewqfmijxycppnjjfp@forum.dlang.org) a run for their money.


More information about the dip.ideas mailing list