Can I do an or in a version block?

Ali Çehreli acehreli at yahoo.com
Wed Mar 7 22:09:35 PST 2012


On 03/07/2012 10:07 PM, James Miller wrote:
> On 8 March 2012 18:38, Tyler Jameson Little<beatgammit at gmail.com>  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?
>
> I don't think there is an 'elseif' for versions, probably because you
> are normally checking mutually exclusive version descriptions.
> Otherwise, its probably a good idea to keep the syntax as is, since it
> stops people from abusing the mechanic.
>
> --
> James Miller

I had played with this in the past. The assignment to version has an 
interesting meaning of "collecting" everything assigned to it:

import std.stdio;

version (linux)
{
     version = linuxOrBSD;
     version = foo;
}

version (BSD)
{
     version = linuxOrBSD;
     version = foo;
}

void main()
{
     version (linuxOrBSD) {
         writeln("linux or BSD");
         // do something...
     } else {
         version (Windows) {
             // do something else
         } else {
             // do something else
             assert(false, "Unsupported operating system");
         }
     }

     // Later on even this works
     version (foo) {
         writeln("even foo!");
     }
}

Ali



More information about the Digitalmars-d-learn mailing list