« Undocumented FLfile | Main | Flex Builder: The IDE for Flex »
July 31, 2004
Multiple Expressions in Case statements and MX 2004
Multiple expressions in Case statements are not supported. For example, following is not supported:case "A","B","C":instead you can test for multiple cases by using multiple Case statements and with one expression for each:
case "A": case "B": case "C":This is the case with Flash MX (Flash 6) also. (You can read more about this at livedocs for switch)
Many people are used to using multiple expressions with Case from other languages, and Flash compiles them without a warning.
What's happening here?
If you have a case like 'case "A","B","C":', Flash pushes each expression and only the last expression (top of the stack) is evaluated. So this works exactly like case "C":, with "A" and "B" left in the stack without any side effects other than increasing code size a few bytes.
Probably the reason we're not getting an error is that it's completely legal to have statements separated by commas.
This brings us to the more subtle case...
Consider the following sample code, which works with Flash MX but produces an unexpected result with Flash MX 2004:
x = "B";
var y;
switch (x) {
case y="A", y :
trace("A");
break;
case y="B", y :
trace("B");
break;
default :
trace("D");
}
With Flash MX, "B" is traced, as expected, but this fails with Flash MX 2004 (which traces "D") because multiple expressions/statements in Case statements are not supported and a register optimization MX 2004 uses without considering the unsupported case, breaks the code. Obviously, this cannot be considered as a bug.
But this is interesting to me because there's no way you can get the above code exported in MX2004 as in MX. Even if you export for SWF 6, you won't get the expected result, and if you export for SWF 5, you'll get "A" traced.
Conclusion: We need more errors/warnings with the compile in 8ball.
July 31, 2004 in Flash | Permalink
TrackBack
TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8341c73d553ef00d8342e894153ef
Listed below are links to weblogs that reference Multiple Expressions in Case statements and MX 2004:
Comments
Gee I don't know, that looks like a bug to me... If switch doesn't support multiple expressions they shouldn't be allowed. If it isn't a bug in the implementation, it is a bug in the language specification then!
This is one of the things that drives me crazy about actionscript -- errors that just silently fail.
Posted by: Robin Debreuil at Aug 1, 2004 6:20:13 AM
Ok, I just looked it up, and it seems like expressions always allow commas, wow!
Expression : AssignmentExpression
Expression , AssignmentExpression
I bet that was the easiest way to allow comma separated declarations without having to actually think about it ; ). So this is legal then:
if(how,silly,is,this)
pretty,silly;
I often wondered why Flash didn't throw more errors, maybe all this time it was because they aren't technically 'errors'.
Posted by: Robin Debreuil at Aug 1, 2004 6:37:34 AM
no more multiple case ...
x = "B";
var y;
switch (x) {
case y="A":
case y="B":
case y:
trace(y);
break;
default :
trace("D");
}
Posted by: Clément Hussenot at Aug 2, 2004 7:01:00 PM
no more multiple case ...
x = "B";
var y;
switch (x) {
case y="A":
case y="B":
case y:
trace(y);
break;
default :
trace("D");
}
Posted by: Clément Hussenot at Aug 2, 2004 7:01:09 PM