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.


