Search code examples
actionscript-3apache-flexflash-cs4flashdevelop

Is this a bug with AS3 conditional compilation?


In our code I have the following, for now please ignore the //* bits;

if (data["someKey"] != null)//*
{
    CONSOLE_OUT.info("Print some stuff.");
    TARGET::myTarget
    {
        var someString:String = data["someKey"] as String;//*
        someController.setSoemthing(someString.indexOf("soemthing") > -1 ? true : false);//*
    }
}

I have set up my FlashCS4 to have the TARGET::myTarget compiler constant set to false, meaning that the code within the compiler constant shouldn't be compiled. At the point of execution data["someKey"] evaluates to null meaning the if statement should NOT execute.

When I debug the following code, the lines with //* on them execute, which is strange behaviour. It skips the initial line after the if statement and goes straight to executing the code that shouldn't have been compiled, bearing in mind that it shouldn't enter the if statement anyway. Its almost as if the presence of the compiler constant is causing the if statement to appear to be a single line, and then still executing the code within the wrong scope.

However, if I add an else statement on the end, the code executes fine;

if (data["someKey"] != null)//*
{
    CONSOLE_OUT.info("Print some stuff.");
    TARGET::myTarget
    {
        var someString:String = data["someKey"] as String;
        someController.setSoemthing(someString.indexOf("soemthing") > -1 ? true : false);
    }
}
else
{
    CONSOLE_OUT.info("Print some other stuff.");
}

It should also be noted that in the instance where data["someKey"] evaluates to something other than null, the above version will correctly skip (or not compile) the code within the constant.

I just want to find out if this is a bug, or if I am not using the correct syntax for the compiler constant. If you need any more information then please let me know. I've double check my compiler constants, I am using Flash CS4 to compile and targeting Flash Player 10 if that makes a difference.


Solution

  • Its not bug, compiler will strip any if(false) statement so your conditional constant must be wrapped in condition evaluation.

    if (data["someKey"] != null)//*
    {
        CONSOLE_OUT.info("Print some stuff.");
        if(TARGET::myTarget) // it should be conditional statement
        {
            var someString:String = data["someKey"] as String;//*
            someController.setSoemthing(someString.indexOf("soemthing") > -1 ? true : false);//*
        }
    }
    

    If you look at flex sample, they have applied symbol outside method declaration, so when you write conditional compilation symbol outside member body, member body is included/excluded, but in case of inline statement, flex has no way to determine what to exclude, so it should be used in condition within if.

    See answers and links here,

    Flash/Flex conditional compilation "else"