Boolean operators and getter functions
Friday, 08 April 2005A while back I came across an obscure bug with the type checking in Macromedia's Actionscript compiler. It seems that the compiler calculates that the result of a boolean expression involving only getter functions is of type Function.
Take the following class:
-
class Test1 {
-
// Field 1 getter
-
public function get field1():Boolean {
-
return true;
-
}
-
-
// Field 2 getter
-
public function get field2():Boolean {
-
return false;
-
}
-
-
// Constructor
-
public function Test() {
-
var test:Boolean = field1 || field2;
-
trace(test);
-
}
-
}
If you try and compile this, you will get the following error:
**Error** [...] Test.as: Line 14: Type mismatch in assignment statement: found Function where Boolean is required.
var test:Boolean = field1 || field2;
This only seems to be a compile time checking issue. If you remove the data type from the test variable the code will compile fine, and the trace statement will output true as it should.
Changing line 14 to...
-
var test:Boolean = field1 == true || field2 == true;
...seems to fix the problem.








Have you reported this bug to MM?
Paul Neave | Friday, 08 April 2005 | 1:45 pmHave you reported this bug to MM?
I've run into a similar problem before, except I don't
Keith Wilcox | Friday, 08 April 2005 | 10:33 pmI’ve run into a similar problem before, except I don’t remember it being related to Booleans. I seem to remember it involved the compiler flipping out about passing a getter into a function which wanted a string datatype.
Yeah, I think I've had that one too. It seems
Steve | Saturday, 09 April 2005 | 8:16 amYeah, I think I’ve had that one too. It seems that the type checking routines in the compiler doesn’t quite handle getters properly. It’s particularly annoying because once you get around the type checking, the code actually works just fine.
Would (field1 || field2) or ((field1) || (field2)) help solve
Tangent | Friday, 27 May 2005 | 1:28 amWould (field1 || field2) or ((field1) || (field2)) help solve the issue?