Inversion of conditional compilation statements
ryuukk_
ryuukk.dev at gmail.com
Sat Dec 2 15:03:25 UTC 2023
On Saturday, 2 December 2023 at 13:16:26 UTC, Johannes
Miesenhardt wrote:
> Hello,
> I am trying to learn D and I have stumbled upon an issue
> Consider this code:
> ```d
> import std.stdio;
>
> //version = Test;
>
> int main() {
> version (Test) {
> writeln("Hello, world!");
> }
> return 0;
> }
> ```
>
> This compiles, however what if we want to turn the version
> statement around?
> I first expected `version (!Test)` to work, but it doesn't
> since the grammar says:
> ```
> VersionCondition:
> version ( Identifier )
> version ( unittest )
> version ( assert )
> ```
>
> We are using the first way, the one with the Identifier.
> The reason inverting works with if-statements is because they
> take an "Expression".
>
> I see the way why it doesn't work, but I think it should.
> Considering that
> `version (Test) {} else {`
> works without any issue but looks very ugly.
>
> Can somebody explain if this is an intended decision or what I
> should do instead of using my ugly replacement?
It depends what do you want to achieve, if you just want a bool
at the top of your module to toggle features, then you can do
this:
```D
import std.stdio;
enum Test = true;
int main() {
static if (Test == false) {
writeln("this will never be called, it won't be
compiled");
} else {
writeln("hello!");
}
return 0;
}
```
``static if`` is evaluated at compile time, you can mix it with
``version`` if you need to provide the flag during compilation
```D
import std.stdio;
version (Test)
enum HasTest = true;
else
enum HasTest = false;
int main() {
static if (HasTest == false) {
writeln("this will never be called, it won't be
compiled");
} else {
writeln("hello!");
}
return 0;
}
```
I wish we could use ``version`` as expression, to void the
repetition:
```D
import std.stdio;
enum HasTest = version (Test) ? true : false;
int main() {
static if (HasTest == false) {
writeln("this will never be called, it won't be
compiled");
} else {
writeln("hello!");
}
return 0;
}
```
If someone able to do it, would be cool to see a PR
More information about the Digitalmars-d-learn
mailing list