Defining a version after it's tested for

Steven Schveighoffer schveiguy at yahoo.com
Thu May 21 09:20:25 PDT 2009


On Thu, 21 May 2009 10:45:24 -0400, Robert Clipsham  
<robert at octarineparrot.com> wrote:

> I recently came across this:
>
> version( BackendFoo ) {}
> else version( BackendBar ) {}
> else version( BackendCar ) {}
> else
> {
>    pragma( msg, "Warning: backend version undefined" );
>    pragma( msg, "Attempting to guess backend" );
>    version( Windows )
>    {
>      version = BackendFoo;
>      pragma( msg, "Selected the Foo backend." );
>    }
>    else version(...)
>    { /* You get the idea */ }
> }
>
> However, when trying to compile I get the following error:
>
> Error: version BackendFoo defined after use
>
> What is the reasoning behind this? It could be extremely useful to have  
> this functionality. The only reason I can think of for doing this is  
> that this code could be evaluated after modules that depend on the  
> version being defined, causing it not to work. Surely there would be a  
> way around that?
>
> If it's not possible to fix this, what way would you recommend I get  
> around this? The only idea I've come up with so far is to do:
>
> else
> {
>    static assert( false, "Please compile with  
> -version=Backend(Foo|Bar|Bar)" );
> }
>
> Which is less than optimal.

Versions are intended to be predefined before usage.  It's the same thing  
as using a variable before it's defined.  The rational is, to flag errors  
to the user where he thinks forward referencing of versions works.

Unfortunately, you have to do the following workaround:

version(BackendFoo) { version=BackendFoo_;}
else version(BackendBar) { version=BackendBar_;}
else version(BackendCar) { version=BackendCar_;}
else
{
   version(Windows) {version=BackendFoo_;}
}

And now, you must use Backend*_ in your code.  It should work, but it's  
ugly.

Maybe Walter has a better method.

-Steve



More information about the Digitalmars-d mailing list