Question

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.

Was it helpful?

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"

OTHER TIPS

I am not sure what you are doing with the TARGET static class.
I have never seen anything like that and without know what TARGET is I wouldn't know how to correct it.
In any case in your if statement you are testing if someKey has a value, however if someKey has not been defined then it wouldn't be null it would be undefined.
With that being said you need to test for it and the proper way to test for it would be like so.

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 the characters "/*" denote the start 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 "i.e" is showing 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

reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top