Running this code
var myValue1:int = 2;
switch (myValue1)
{
case -3: trace(myValue1 + " == -3"); break;
case -2: trace(myValue1 + " == -2"); break;
case -1: trace(myValue1 + " == -1"); break;
case 0: trace(myValue1 + " == 0"); break;
case 1: trace(myValue1 + " == 1"); break;
case 2: trace(myValue1 + " == 2"); break;
case 3: trace(myValue1 + " == 3"); break;
default: trace(myValue1 + " is unknown"); break;
}
var myValue2:int = -2;
switch (myValue2)
{
case -3: trace(myValue2 + " == -3"); break;
case -2: trace(myValue2 + " == -2"); break;
case -1: trace(myValue2 + " == -1"); break;
case 0: trace(myValue2 + " == 0"); break;
case 1: trace(myValue2 + " == 1"); break;
case 2: trace(myValue2 + " == 2"); break;
case 3: trace(myValue2 + " == 3"); break;
default: trace(myValue2 + " is unknown"); break;
}
gives this output:
2 == 0 -2 is unknown
(Compiled in Flash Builder 4.7.0.349722, running on Flash 11.5.502.149. Running in Windows 7 SP1, Firefox 18.0.2)
The following changes all fix the above problem, giving the correct output:
- Changing the value-type to
Number
. - Removing the negative-number
case
statements. - Changing the
case
statements to useint
-variables rather than literals... unless those variables are alsoconst
, in which case it stays broken!
Changing myValue2 = -1
gives the output -1 == -3
, which is equally wtf-ish.
Clearly this is a bug, but... what causes it? Is there some subtle nuance of using int
or negative numbers in case-statements that I don't understand? Is my code somehow wrong? Or is this simply an issue with the bytecode-compiler in Flash Builder?