Allow Conditional Compilation Inside Enum Declaration
Basile B.
b2.temp at gmx.com
Tue Apr 2 17:30:39 UTC 2024
On Tuesday, 2 April 2024 at 17:10:07 UTC, Basile B. wrote:
> On Saturday, 30 March 2024 at 14:57:00 UTC, IchorDev wrote:
>> To declare an enum type that may or may not have one or more
>> members depending on conditional compilation statements
>> requires duplicating the entire enum:
>> ```d
>> static if(cond){
>> enum A{
>> x,y,z,w,
>> }
>> }else{
>> enum A{
>> x,y,z,
>> }
>> }
>> ```
>>
>> 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 agree and the idea is not novel, [for example that], from
> memory it's been seen several times in the NG too. Risk for a
> DIP on this is that it could get rejected with a rationale such
> as "you can do that with metaprog, which is the D way". That is
> more or less what what Paul has replied, if you read under the
> lines.
>
> [for example that]:
> https://issues.dlang.org/show_bug.cgi?id=9761
forgot to say you can use a struct filled with anonymous enum
members too.
```d
struct A
{
enum T x = 0;
enum T y = 1;
enum T z = 2;
static if (cond)
enum T w = 3;
}
```
However RN I cant confirm that the usage is 100% equivalent. Use
of the members is likely but use of the container may not, e.g in
std.traits (esp. the EnumMembers template ...).
More information about the dip.ideas
mailing list