Search code examples
apache-flexlanguage-features

Flex : Assignment in Conditionals


If I use an Assignment within conditional, for e.g.

if(userType='admin')

Flex throws a warning, unlike Java, which throws an error. Why should Flex be doing this, despite being one of the newest languages?

1100: Assignment within conditional. Did you mean == instead of =?


Solution

  • Because assignments have a value in Actionscript, which makes that syntax legal, and they don't have a value in Java, which makes it not. The difference comes because despite recent Java-izations, Actionscript is descended from ECMAScript. Other consequences of this design are the ability to make statements like this:

    var foo:Number = 0;
    var bar:Number = 0;
    foo = bar = 2;
    assertEquals(2, foo);
    assertEquals(2, bar);
    

    IMO, this is the best behavior it could have - it doesn't break compatibility with older versions of Actionscript, and it doesn't remove language functionality for the purpose of handholding, but it does bring a common error to the attention of the user.