Cast error in OpenFl

Hello,

this code work in as3

class TextureMaskStyle extends MeshStyle
{
    override public function copyFrom(meshStyle : MeshStyle) : void
    {
		var otherStyle : TextureMaskStyle = meshStyle as TextureMaskStyle;
	}
}

but I have an error in OpenFl

class TextureMaskStyle extends MeshStyle
{
    override public function copyFrom(meshStyle : MeshStyle) : Void
    {
		var otherStyle : TextureMaskStyle = cast(meshStyle, TextureMaskStyle);
	}
}

There’s a difference in how cast works between ActionScript 3 and Haxe, Haxe has two forms of casting:

var otherStyle:TextureMaskStyle = cast meshStyle;
var otherStyle = cast (meshStyle, TextureMaskStyle);

The first one will force a cast, which costs nothing at runtime, but might crash if the object is not the correct type.

The second one will check the cast at runtime, which incurs a slight cost. If it does not match, it will throw a runtime error.

The behavior of AS3 as will return null, and not throw an error, if the object does not match the requested type. In order to replicate this fully, you would use something like:

var otherStyle:TextureMaskStyle = Std.is (meshStyle, TextureMaskStyle) ? cast meshStyle : null;

Long-term, most Haxe projects tend to move away from use of Dynamics where possible as it hurts performance and can lead to rough edges, but it certainly is something that comes up when first porting :slight_smile: