Good examples of version() algebra in real code

Hipreme msnmancini at hotmail.com
Mon May 22 11:12:49 UTC 2023


On Monday, 22 May 2023 at 08:23:31 UTC, Walter Bright wrote:
> ```
> version(linux)         enum ExtraFunctionality = true;
> else version(OSX)      enum ExtraFunctionality = true;
> else version(Windows)  enum ExtraFunctionality = false;
> else static assert(0, "system not accounted for");
> ```
>
> (forgot to use Markdown! Oops!)

When I programmed for Haxe, it did have a feature which you could 
for example name your file based on the target OS:

Imagine we had the `module input`. Since all OS have a different 
way to get input, we can basically separate them as you just 
said, by files. How Haxe solves that is by letting users define a 
special file name such as:
`input.windows.hx` and `input.javascript.hx`. Whenever you import 
that file for usage, it will automatically import the correct 
file based on your target. I did use that feature and in my 
experience: This is quite cool and can lead to a single file to 
read without having any extra concern. The problem is that it did 
generated a bit of code duplication. Which could be solved by 
putting the common code in another file.




More information about the Digitalmars-d mailing list