cammil
cammil

Reputation: 9939

Is it possible to set static constants using conditional compilation?

I am trying to set some public static constants on a class conditionally by passing variables to the compiler e.g. -define=CONFIG::ENVIRONMENT,'testing_server'

This is what I'd like to do:

if(CONFIG::ENVIRONMENT=='production')
    public static const DOMAIN:String="production_site.com";
else if(CONFIG::ENVIRONMENT=='testing_server')
    public static const DOMAIN:String="secret_domain.com";

I have tried many versions of this code, but everything so far has produced an error of one sort or another. The only way I have succeeded is by setting a compiler variable for each environment (all false except the one wanted which is true) and using the following syntax:

CONFIG::PRODUCTION{
    public static const DOMAIN:String="production_site.com";
}
CONFIG::TESTING_SERVER{
    public static const DOMAIN:String="secret_domain.com";
}

Obviously this means I have to tinker with long command line setting each time. I thought my initial approach was possible given the documentation and various tutorials I have read.

Can anyone help?

Upvotes: 3

Views: 206

Answers (2)

Rydell
Rydell

Reputation: 4217

It is. I'm doing something similar in my code:

trace('appMode = ' + CONFIG::appMode); 
if (CONFIG::appMode == "dev" || CONFIG::appMode == "tst") { 
    // if dev or tst
} else {
    // if anything else
}

I assign the constant appMode in fb4 using the "Additional Compiler Arguments" field in the Flex Compiler properties (project -> properties -> Flex Compiler). The argument looks like:

-define+=CONFIG::appMode,"'dev'"

Note the nested double/single quotes around dev.

Good Luck!

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74949

One thing that has worked well for us in this situations is #include. Use the build script to generate a single line of AS which has the declaration. Then use #include to include the generated AS file in a class.

I wouldn't recommend widespread use of includes, they're ugly and can lead to problems, but very small usages can be useful in certain situations.

Upvotes: 1

Related Questions