3

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.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
JimmyDeemo
  • 313
  • 1
  • 15
  • Did you copy-paste any of this code into your project? Could be it has some strange characters hiding in there unexpectedly messing with you. – Sam DeHaan Mar 22 '12 at 19:49
  • 1
    Apparently people don't know about Flash's [conditional compilation or its syntax](http://help.adobe.com/en_US/Flash/10.0_UsingFlash/WS3e7c64e37a1d85e1e229110db38dec34-7fa4a.html#WS7D94A7C3-8F91-421a-936C-F076374C470F). – zzzzBov Mar 22 '12 at 19:50
  • I tested out your example (or at least something near to it - I don't have Flash CS4, only Flash Builder), and didn't get any strange behaviour. I can only recommend trying to reproduce the behaviour in as simple a test case as possible to try and find the weird hidden thing that is causing this. – Wesley Petrowski Mar 22 '12 at 20:27
  • I too am confused why the above doesn't work, and I will try to replicate with a simple example. It seems @AkashKava 's example might be the most elegant solution, but I have not tried it yet. – JimmyDeemo Mar 23 '12 at 07:56

2 Answers2

2

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"

Community
  • 1
  • 1
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
-3

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

The_asMan
  • 6,364
  • 4
  • 23
  • 34
  • 2
    if you don't know how to use compilation constants, don't answer the question. `TARGET::myTarget` would be defined/undefined on the project level before the actionscript is compiled into a `swf`. – zzzzBov Mar 22 '12 at 16:58
  • [you need to read this](http://help.adobe.com/en_US/Flash/10.0_UsingFlash/WS3e7c64e37a1d85e1e229110db38dec34-7fa4a.html#WS7D94A7C3-8F91-421a-936C-F076374C470F). – zzzzBov Mar 22 '12 at 16:59
  • @zzzzBov You're right, but I think he makes a valid point about the comments nevertheless. This may very well be the answer to the question. – RIAstar Mar 22 '12 at 19:16
  • @RIAstar, his point about the `/*` comments was updated after my first comment. Unfortunately, he's wrong on that too: `/*` *does* start a multi-line comment, but `//*` **does not**. It's a single-line comment, followed by an asterisk. Additionally, it was noted as being used as a code marker for the question only. – zzzzBov Mar 22 '12 at 19:21
  • @zzzzBov I stand corrected. That should be another -1 then ;) – RIAstar Mar 22 '12 at 19:39
  • Beware. Ones does not simple ask a question... :D –  Mar 22 '12 at 22:43
  • Yeah sorry @The_asMan but I'm using a compiler constant here, for conditional compilation. Fair point about the `//*`, although I was just using it as a pointer to which lines got executed, I could have used something that was less ambiguous. – JimmyDeemo Mar 23 '12 at 08:02
  • Also, and correct me if I'm wrong, but I thought that `undefinded == null` evaluates to `true`, meaning I'm OK to do `data["someKey"] != null`? – JimmyDeemo Mar 23 '12 at 08:07
  • @zzzzBov and RIAstar before you comment and mark answers up or down why don't you start with making sure you understand what you are preaching. Go ahead and look at the answer that was marked correct. Don't bother responding to this I am done with you both. – The_asMan Mar 23 '12 at 15:22
  • @JimmyDeemo Not sure why I am bothering but I editted my answer to show you some facts about undefined and null. – The_asMan Mar 23 '12 at 15:37