Allow Conditional Compilation Inside Enum Declaration
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Tue Apr 2 17:34:26 UTC 2024
On Tuesday, April 2, 2024 11:19:54 AM MDT Hipreme via dip.ideas wrote:
> On Sunday, 31 March 2024 at 02:36:40 UTC, IchorDev wrote:
> > On Saturday, 30 March 2024 at 22:34:46 UTC, monkyyy wrote:
> >> As a [specific] case or for all lists?
> >>
> >> Could you do this for functions `foo(static if(bar){1},2)`?
> >
> > I’m not sure if that’s really in the scope of this idea. Maybe
> > that could be another proposal?
> >
> >> is changing the meaning of {} nessery or should it be for
> >> single elements?
> >> `enum A{x,y,z, static if(cond) w}`
> >
> > I don’t see why that would be necessary? Ideally it should work
> > just like any existing conditional compilation. (e.g. also
> > allowing `version(Something):`, etc.)
>
> My main take against that, is that it would be harder to make it
> compatible when using libraries. One value could change:
>
> ```d
> enum A
> {
> static if(cond) w,
> x,
> y,
> z
> }
> ```
>
> And boom! Depending on how you're using, one may:
>
> 1. Break
> 2. Worse: create surprising behavior because, if you use X, it
> will become Y which is still valid.
>
> Although the idea does make sense in some aspects, I fear for the
> worst
Well, you can already do that. It's just that you have to wrap the entire
enum declaration to do it. Really, this is exactly the same as using static
ifs to compile members into a struct or not, which we do all the time. So,
there's nothing special here, and the fact that we can't do it with enums
regularly causes issues any time that you're dealing with enum declarations
that need to vary based on stuff like the OS (both for when the list of enum
members needs to change and when the values of those members needs to change
depending on the system). Fortunately, most enums aren't in that boat, but
it surprises me every time that I run into it.
I suspect that enum declarations are the only place in the language where
you can put ddoc comments on individual symbols, but you can't actually
version those symbols with version statements or static ifs. And it
definitely feels inconsistent that we're not allowed to use conditional
compilation with their declarations.
And yes, it could be abused, but that's no different from any other place
that conditional compilation is used to control whether symbols get compiled
in or not.
- Jonathan M Davis
More information about the dip.ideas
mailing list