www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Neater "not version (...)" ?

reply Vladimirs Nordholm <v vladde.net> writes:
Hello.

I wonder if there is a better way to compile something if the 
current operating system is _not_ a specific platform.

For example, I only want some code to compile if the operating 
system is not Windows. Currently I do this:

     version (Windows)
     {
     }
     else
     {
         // ... my code
     }

Is there a neater version of this, like `!version (Windows) { /+ 
... my code +/ }` ?
Sep 16 2020
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Wednesday, 16 September 2020 at 17:53:31 UTC, Vladimirs 
Nordholm wrote:
 Hello.

 I wonder if there is a better way to compile something if the 
 current operating system is _not_ a specific platform.

 For example, I only want some code to compile if the operating 
 system is not Windows. Currently I do this:

     version (Windows)
     {
     }
     else
     {
         // ... my code
     }

 Is there a neater version of this, like `!version (Windows) { 
 /+ ... my code +/ }` ?
Not what you may want, but a dub solution is available. "excludeSourceFiles-windows" : ["source/someunixcode.d"]
Sep 16 2020
parent Vladimirs Nordholm <v vladde.net> writes:
On Wednesday, 16 September 2020 at 18:07:25 UTC, Ferhat Kurtulmuş 
wrote:
 On Wednesday, 16 September 2020 at 17:53:31 UTC, Vladimirs 
 Nordholm wrote:
 Hello.

 I wonder if there is a better way to compile something if the 
 current operating system is _not_ a specific platform.

 For example, I only want some code to compile if the operating 
 system is not Windows. Currently I do this:

     version (Windows)
     {
     }
     else
     {
         // ... my code
     }

 Is there a neater version of this, like `!version (Windows) { 
 /+ ... my code +/ }` ?
Not what you may want, but a dub solution is available. "excludeSourceFiles-windows" : ["source/someunixcode.d"]
Unfortunately for my use case this does not work here. Thanks for the tip though!
Sep 16 2020
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2020-09-16 19:53, Vladimirs Nordholm wrote:
 Hello.
 
 I wonder if there is a better way to compile something if the current 
 operating system is _not_ a specific platform.
 
 For example, I only want some code to compile if the operating system is 
 not Windows. Currently I do this:
 
      version (Windows)
      {
      }
      else
      {
          // ... my code
      }
 
 Is there a neater version of this, like `!version (Windows) { /+ ... my 
 code +/ }` ?
The workaround for that is to define booleans for all versions and then use `static if`: version (Windows) enum windows = true; else enum windows = false; static if (!windows) { // ... my code } -- /Jacob Carlborg
Sep 16 2020
parent reply Vladimirs Nordholm <v vladde.net> writes:
On Wednesday, 16 September 2020 at 18:54:45 UTC, Jacob Carlborg 
wrote:
 On 2020-09-16 19:53, Vladimirs Nordholm wrote:
 Hello.
 
 I wonder if there is a better way to compile something if the 
 current operating system is _not_ a specific platform.
 
 For example, I only want some code to compile if the operating 
 system is not Windows. Currently I do this:
 
      version (Windows)
      {
      }
      else
      {
          // ... my code
      }
 
 Is there a neater version of this, like `!version (Windows) { 
 /+ ... my code +/ }` ?
The workaround for that is to define booleans for all versions and then use `static if`: version (Windows) enum windows = true; else enum windows = false; static if (!windows) { // ... my code }
Ah, I guess it boils down to this then. Doesn't really make it "neater", but thank you for the tip!
Sep 16 2020
next sibling parent Simen =?UTF-8?B?S2rDpnLDpXM=?= <Simen.kjaras gmail.com> writes:
On Wednesday, 16 September 2020 at 19:04:24 UTC, Vladimirs 
Nordholm wrote:
 On Wednesday, 16 September 2020 at 18:54:45 UTC, Jacob Carlborg 
 wrote:
 version (Windows)
     enum windows = true;
 else
     enum windows = false;

 static if (!windows)
 {
     // ... my code
 }
Ah, I guess it boils down to this then. Doesn't really make it "neater", but thank you for the tip!
I wrote this helper a little while back: struct Version { template opDispatch(string name) { mixin("version ("~name~") enum opDispatch = true; else enum opDispatch = false;"); } } static if (Version.Windows) pragma(msg, "Windows machine"); static if (Version.linux) pragma(msg, "Linux machine"); Note that it only works for global versions, and those defined in the same module as Version. -- Simen
Sep 16 2020
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
On Wednesday, 16 September 2020 at 19:04:24 UTC, Vladimirs 
Nordholm wrote:
 Ah, I guess it boils down to this then. Doesn't really make it 
 "neater", but thank you for the tip!
IMO, just keep it as `version(Windows) {} else { ... }` if you HAVE to instead of one of the workarounds people suggest. I do wonder what kind of code runs on the "not Windows" operating system though, do you mean `version(Posix)` perhaps?
Sep 16 2020
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2020-09-16 21:04, Vladimirs Nordholm wrote:

 Ah, I guess it boils down to this then. Doesn't really make it "neater", 
 but thank you for the tip!
You only need to declare the enums ones. -- /Jacob Carlborg
Sep 18 2020