Can I do an or in a version block?

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 7 22:12:44 PST 2012


On Thursday, March 08, 2012 06:38:48 Tyler Jameson Little wrote:
> I would like to do something like this:
> 
> version (linux || BSD) {
>      // do something...
> } else {
>      version (Windows) {
>          // do something else
>      } else {
>          // do something else
>          assert(false, "Unsupported operating system");
>      }
> }
> 
> The only way I've been able to do this, is by splitting up the
> two versions and repeat code.
> 
> Is there a better way to do this? A static if can do this, so is
> there a way that I can use a static if somehow?

You can do

version(x) {}
else version(y) {}
else {}

but there is no logical and or logical or with versions. It's _only_ checking 
whether a particular version exists or not. Walter Bright is completely 
against having anything more complicated with versioning, since he thinks that 
it just engenders bad code and bugs.

Now, you could do

version(x)
    version = xOrY
else version(y)
    version = xOrY

version(xOrY) {}

and then xOrY will exist for the rest of that module. But that's as close as 
you're going to ge to logical and or logical or for versions.

Of course, if the issue is linux || FreeBSD, you might want to just consider 
using Posix. Unless you're doing something that is specific to linux and 
FreeBSD but not Posix in general (which I would expect to be unlikely), Posix 
will do the trick just fine.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list