Allow Conditional Compilation Inside Enum Declaration
Timon Gehr
timon.gehr at gmx.ch
Thu Apr 25 17:22:02 UTC 2024
On 4/2/24 00:00, Paul Backus wrote:
> On Saturday, 30 March 2024 at 14:57:00 UTC, IchorDev wrote:
>> The logical solution is to just allow conditional compilation
>> statements inside enums:
>> ```d
>> enum A{
>> x,y,z,
>> static if(cond){
>> w,
>> }
>> }
>> ```
>
> It's not very elegant, but you can do it with a string mixin:
>
> ```d
> enum string enumMembers = "x, y, z, " ~ (cond ? "w, " : "");
>
> mixin("enum A { " ~ enumMembers ~ " }");
> ```
A drawback of this approach is that the enum name is only visible after
the mixin has been expanded, which can lead to forward reference issues.
More information about the dip.ideas
mailing list