static assert(version(x)) ?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Nov 26 12:53:02 UTC 2019


On Tuesday, November 26, 2019 4:29:18 AM MST S.G via Digitalmars-d-learn 
wrote:
> On Tuesday, 26 November 2019 at 10:24:00 UTC, Robert M. Münch
>
> wrote:
> > How can I write something like this to check if any of a set of
> > specific versions is used?
> >
> > static assert(!(version(a) | version(b) | version(c)):
> >
> > The problem is that I can use version(a) like a test, and the
> > symbol a is not accessbile from assert (different,
> > non-accessible namespace).
>
> BTW D language designers are against boolean eval of version.
> It's not a technical restriction, it's just that they don't want
> this to work.

Basically, Walter considers it to be a prime source of bugs in C/C++ code.
druntime, Phobos, etc. consistently do stuff like

version(Posix)
{
}
else version(Windows)
{
}
else
    static assert(false, "platform unsupported);

when it needs code to differ depending on platform or architecture or
whatever. And if that means duplicating some code in each version block,
then it means duplicating some code in each version block. static if can be
used instead of version blocks to get boolean conditions, and local version
identifiers can be defined which combine some set of version identifiers,
but such practices are discouraged for D programmers in general, and they're
basically forbidden in official source code. The only case I'm aware of
where anything like that is used in druntime or Phobos is for darwin stuff,
since darwin isn't a predefined identifier.

- Jonathan M Davis






More information about the Digitalmars-d-learn mailing list