Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
November 26, 2019 static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
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).
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
|
November 26, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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).
version(A) { enum isA = true; } else { enum isA = false; }
version(B) { enum isB = true; } else { enum isB = false; }
static assert(!(isA || isB));
|
November 26, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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.
|
November 26, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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? ``` version(a) {} else version(b) {} else version(c) {} else { static assert(0, "only versions a, b and c are supported"); } ``` ``` version(a) version = supported; version(b) version = supported; version(c) version = supported; version(supported) { // good to go } else { static assert(0, "not a supported version"); } ``` > static assert(!(version(a) | version(b) | version(c)): That seems to be the opposite of what you describe. If you want that, then: ``` version(a) static assert(0, "version a not supported"); version(b) static assert(0, "version b not supported"); version(c) static assert(0, "version c not supported"); ``` |
November 26, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to S.G | 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
|
November 27, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 26 November 2019 at 12:53:02 UTC, Jonathan M Davis wrote: > 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. > > ... > 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. `xversion` is a simple and effective and useful tool, used in dmd source: https://github.com/dlang/dmd/blob/53b533dc7fc5da604e7ebf457734766b4e96d900/src/dmd/globals.d#L21-L35 ``` template xversion(string s) { enum xversion = mixin(`{ version (` ~ s ~ `) return true; else return false; }`)(); } enum version_a = xversion!`a`; ``` -Johan |
November 28, 2019 Re: static assert(version(x)) ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | On 2019-11-27 18:50:07 +0000, Johan Engelen said: > On Tuesday, 26 November 2019 at 12:53:02 UTC, Jonathan M Davis wrote: >> 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. >> >> ... >> 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. > > `xversion` is a simple and effective and useful tool, used in dmd source: That's pretty neat... going to try this. -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
Copyright © 1999-2021 by the D Language Foundation