Order of Operations Strikes Again

Quick AS3 hint for today. Keep learning from my mistakes! Did you know that the order of the ! operator is higher than the order of the is operator? That means that in the example:

if (!strategy is AwesomeStrategy) {...

in which I am trying to run some code if the strategy is not an instance of AwesomeStrategy, actually evaluates:

if (null is AwesomeStrategy) {...
if (false) {...

In other words, the not binds higher and applies to strategy instead of strategy is AwesomeStrategy, the condition evaluates to false 100% of the time, and you end up puzzled as to why the block in the if statement didn’t run. Don’t make my mistake! Include parentheses when negating comparisons! Use this instead:

if (!(strategy is AwesomeStrategy)) {...

That’s right, you heard it here first. Unless you aren’t as tired as me right now and already knew that. Actually, as I look at my incorrect code now, it seems pretty obviously incorrect. OH WELL!

See the whole order of operations in AS3 here.

This entry was posted in AS3, Flash, Flex, Programming, Tutorial. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree