This morning I came across some strange sort-of overload workaround that can cast your class to any type. Imagine you want to have a reference to a boolean. You have to “box” it then. It look like this:
[as]class BoxedBoolean
{
private var value: Boolean;
public function BoxedBoolean( value: Boolean )
{
this.value = value;
}
}[/as]
Now doing something like if ( boxedBoolean == true ) is not possible since boxedBoolean is type BoxedBoolean. Even though the value we want to use is a boolean value. So here is the workaround:
[as]class BoxedBoolean
{
private var value: Boolean;
public function BoxedBoolean( value: Boolean )
{
this.value = value;
}
public function toString(): Boolean
{
return value;
}
}[/as]
Do not ask me what strange nature toString() has but it works. Now the only problem with this is that if ( a == true ) works fine but if ( a ) is always true.
Here is an example
[as]var b0: BoxedBoolean = new BoxedBoolean( true );
var b1: BoxedBoolean = new BoxedBoolean( false );
trace( b0 is Boolean );
trace( b1 is Boolean );
trace( b0 );
trace( b1 );
if ( b0 == true )
trace( ‘b0 = true’ );
else
trace( ‘b0 = false’ );
if ( b1 == true )
trace( ‘b1 = true’ );
else
trace( ‘b1 = false’ );[/as]
The trace output is then
false
false
true
false
b0 = true
b1 = false
If you remove the == true you will get that both b0 and b1 are true. I do not think that there is much use for it but it is interesting what you can do. toString() can also return an integer and it works as well.




This problem is simple, == always does the comparision between the toString() representation of two objects if they are of different type. === does strict comparision, which in turn wont work here.
“== always does the comparision between the toString() representation of two objects if they are of different type.”
That is wrong. Try class A and class B both have toString returning the same. Anyways if you just try something like trace( a == b ); you get a compile-time error because comparison of two different types is made.
Also if you want to compare a to ” and toString() returns ” you get the same error.
I understand the Machine uses the .toString() method to convert a type to something it can use in a consistent way when computing an equation.
a == true is a loose comparison, so the Machine will convert the “x” type to String and then evaluate.
a === true is a strict comparison, so the Machine will actually check if the type is the same first.
if(a) is a little different, as it will return true if the object is not:
- null;
- undefined;
- false;
- evaluates to false, like ” or 0;
The latter statement is usually used to check if something exists, but it’s dirty, and AS3 gives us new means of checking if a variable exists and if it is populated.
At least, I think.. :P