Allow Conditional Compilation Inside Enum Declaration

Basile B. b2.temp at gmx.com
Thu Oct 31 18:31:55 UTC 2024


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,
> 	}
> }
> ```

Yesterday I was thinking to add that feature to another language 
(understand "one where there's no implication", you can 
experiment and "nobody will loose money") and realized a problem 
that's not been raised before:

That would make the grammar significantly more complex. For now 
the body of a "EnumDeclaration" can only contain "EnumMembers", 
whereas if you permit "StaticIfDecl" or "VersionDecl" then you 
also have to special case those in order to be sure that they can 
only contain EnumMembers.


More information about the dip.ideas mailing list