version(number) is completely useless

jfondren julian.fondren at gmail.com
Wed Jul 20 01:38:41 UTC 2022


On Wednesday, 20 July 2022 at 01:22:00 UTC, Andrey Zherikov wrote:
> I actually have a question since this topic is brought up:
> What is conceptual difference between `version(FOO)` and 
> `static if(FOO)`? I see `version` is a very limited comparing 
> to `static if` - the former checks for "boolean" result 
> (whether an ID is defined) while the latter evaluates condition 
> expression.

Consider:

```d
enum Test = true;

void main() {
     import std.stdio : writeln;

     version(Test) {
         writeln("true");
     } else {
         writeln("false");
     }
}
```

And this output:

```
$ dmd -run example.d
false
$ dmd -version=Test -run example.d
true
```

A few seconds of hacking with a build system though, and static 
if seems to be enough:

```d
import mbs = magic_buildsystem_definitions;

enum Test = true;

void main() {
     import std.stdio : writeln;

     static if (mbs.Test) {
         writeln("true");
     } else {
         writeln("false");
     }
}
```

with that module generated of course by the build system, and any 
logic you like now capable.

version's limited expressiveness is due to experience with 
`#ifdef` abuse in C, much like (I imagine) limitations like "the 
code won't even compile if you're not using an import" exists in 
Go. You could positively say that such limitations are lessons 
learned and you could negatively say that they're expressions of 
trauma.


More information about the Digitalmars-d mailing list