version(number) is completely useless

Hipreme msnmancini at hotmail.com
Wed Jul 20 03:18:15 UTC 2022


On Wednesday, 20 July 2022 at 01:38:41 UTC, jfondren wrote:
> 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.


It is virtually impossible to recreate the mess macros done. They 
were so messy because one could define AND undefine them, which 
made tracking a lot hard.

Even worse, macros were used for everything. Constants, inline 
functions, templates, syntax transformers, aliases, conditional 
compilation, importing.

D has already solved that by creating specific tool for each 
thing, giving a way to define version as a namespaced number 
would hardly make anything more complex. If you take the use case 
i bring for example, you you actually check it would make it 
easier to understand


More information about the Digitalmars-d mailing list