开发者

Conditional compilation "else"

In AS3 you can pass a constant to the compiler

-define+=CONFIG::DEBUG,true

And use it for conditional compilation like so:

CONFIG::DEBUG {
   trace("This only gets compiled when debug is true.");
}

I'm looking for something like #ifndef so I can negate the value of debug and use it to conditionally add release code. The only solution I've found so far was in the conditional compilation documentation at adobe and since my debug and release 开发者_如何学Goconfigurations are mutually exclusive I don't like the idea of having both DEBUG and RELEASE constants.

Also, this format works, but I'm assuming that it's running the check at runtime which is not what I want:

if (CONFIG::DEBUG) {
   //debug stuff
}
else {
   //release stuff
}

I also considered doing something like this but it's still not the elegant solution I was hoping for:

-define+=CONFIG::DEBUG,true -define+=CONFIG::RELEASE,!CONFIG::DEBUG

Thanks in advance :)


This works fine and will strip out code that won't run:

if (CONFIG::DEBUG) {
   //debug stuff
}
else {
   //release stuff
}

BUT this will be evaluated at runtime:

if (!CONFIG::DEBUG) {
   //release stuff
}
else {
   //debug stuff
}

mxmlc apparently can only evaluate a literal Boolean, and not any kind of expression, including a simple not.


Use the if / else construct : the dead code will be removed by the compiler and it will not be tested at runtime. You will have only one version of your code in your swf.

If you are not sure use a decompiler or a dump tool to see what really happens.

http://apparat.googlecode.com/files/dump.zip

http://www.swftools.org/

...


While Patrick's answer fulfills the question's criteria, it does not cover all use cases. If you are in an area of code that allows you to use an if/else statement then this is a good answer. But if you are in a place where you cannot then you will need a better solution. For example, you may want to do something like this to declare a constant in a class:

private var server:String = "http://localhost/mystagingenvironment";

or for a live release:

private var server:String = "http://productionserver.com";

(this is an example and I'm not advocating this as production code).

I use xml configs and use the loadConfig+="myconfig.xml" to do my configuration instead of passing large numbers of command line params. So in the <compiler> section of your xml config:

<define>
    <name>CONFIG::debug</name>
    <value>false</value>
  </define>
<define>
    <name>CONFIG::release</name>
    <value>!CONFIG::debug</value>
</define>

This works well for all use cases:

CONFIG::debug
{
    private var server:String = "http://localhost/mystagingenvironment";
}
CONFIG::release
{
    private var server:String = "http://productionserver.com";
}

This has the additional benefit of working consistently across applications. It also does not rely on the 'optimize' flag being true, like Patrick's answer (although I think we can assume that 99.999999% of all swfs have optimize=true, I only set it to false when the optimizer breaks my AS3).

It does have the drawback that it doesn't compile all code paths, just the ones that are included. So if you're not using a build server to create release builds and tell you when things break, be prepared for surprise errors when you do your release build ("But it compiled in debug! Crap, I need this to launch now!").


Just my two cents about Chris Hill's answer (which is the solution I also use regularly): it seems that using the loadConfig+="myconfig.xml" option makes the compiler searching for the myconfig.xml file in the Flex SDK directory whereas the -load-config+=myconfig.xml option makes it searching for the myconfig.xml file in the project's directory, which is the behavior I strongly prefer as you can then easily distribute this file with your project sources...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜