digitalmars.D.learn - static assert(version(x)) ?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (9/9) Nov 26 2019 How can I write something like this to check if any of a set of
- Andrea Fontana (5/11) Nov 26 2019 version(A) { enum isA = true; } else { enum isA = false; }
- S.G (5/11) Nov 26 2019 BTW D language designers are against boolean eval of version.
- Jonathan M Davis (22/35) Nov 26 2019 Basically, Walter considers it to be a prime source of bugs in C/C++ cod...
- Johan Engelen (14/39) Nov 27 2019 `xversion` is a simple and effective and useful tool, used in dmd
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (6/33) Nov 28 2019 That's pretty neat... going to try this.
- Dennis (27/30) Nov 26 2019 ```
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
Nov 26 2019
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));
Nov 26 2019
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.
Nov 26 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: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 DavisHow 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.
Nov 26 2019
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:`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`; ``` -JohanOn Tuesday, 26 November 2019 at 10:24:00 UTC, Robert M. Münch wrote:... 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.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.
Nov 27 2019
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:That's pretty neat... going to try this. -- Robert M. Münch http://www.saphirion.com smarter | better | fasterOn Tuesday, November 26, 2019 4:29:18 AM MST S.G via Digitalmars-d-learn wrote:`xversion` is a simple and effective and useful tool, used in dmd source:On Tuesday, 26 November 2019 at 10:24:00 UTC, Robert M. Münch wrote:... 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.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.
Nov 28 2019
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"); ```
Nov 26 2019