Is this a bug with AS3 conditional compilation?

In our code, I have the following: for now, please ignore the bit //*

;

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 set my FlashCS4 so that the compiler constant is TARGET::myTarget

set to false

, which means that the code inside the compiler constant shouldn't compile. At execution time data["someKey"]

, the value is evaluated null

, which means that the if statement should NOT be executed.

When I debug the following code, the lines from //*

on to them get executed, which is strange behavior. It skips the initial line after the statement if

and directly executes code that shouldn't have been compiled, given that it shouldn't go into a statement if

. Its almost as if having a compiler constant caused the statement to if

appear as one line and then still execute the code in the wrong scope.

However, if I add a statement else

to the end, the code does 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 case where data["someKey"]

evaluating to a nonzero value, the above version will correctly skip (or not compile) the code inside the constant.

I just want to know if this is a bug or if I am not using the correct syntax for the compiler constant. If you need more information, please let me know. I double check my compiler constants, I am using Flash CS4 to compile and configure Flash Player 10 if it matters.

+3


source to share


2 answers


Its not a bug, the compiler will strip any if (false) statement, so your conditional constant must be wrapped when evaluating the state.

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 the flex sample, they applied an external method declaration, so when you write a conditional compilation symbol outside of the member body, the member is included / excluded, but in the case of an inline statement, flex cannot figure out what to exclude, so it should be used in the condition inside the if.



See answers and links here.

Flash / Flex conditional compilation "else"

+2


source


I'm not sure what you are doing with the static TARGET class.
I have never seen anything like this and didn’t know what TARGET is, I don’t know how to fix it.
Anyway, in your if statement you are checking if someKey has a value, however if someKey was not defined it would not be null, it would undefined

.
With that said, you need to test it and properly test it would be like this.

if( data.hasOwnProperty("someKey"){
    CONSOLE_OUT.info("Print some stuff.");
    TARGET::myTarget <<<<<<< WTH is this????????
    {
        var someString:String = data["someKey"] as String;
        someController.setSoemthing(someString.indexOf("soemthing") > -1 ? true : false);
    } }

      

Also note that "/ *" marks the beginning of a comment block, and all code after that will be commented out.
for example

/* this would be commented
this would be commented
*/
this line would not be commented

      



[EDIT]
Notice how the first "ie" shows the property as undefined.

trace(null == undefined); //Outputs true
trace(null === undefined); //Outputs false

var i:Object;
trace(i); //Outputs null
i = {};
trace(i); //Outputs [object Object]

var i:Object = {a:"b", c:"d"};
trace(i.e); //Outputs undefined
i.e = "f";
trace(i.e); //Outputs f

      

link

-3


source







All Articles