Hi Haxe people!
I have what I think is a basic problem here whose root cause is that I don't completely understand abstracts in Haxe yet. Here's the situation:
I have a class in a package, Shape (not to be confused with flash.display.Shape), and another class in another package, AngledRectangle. I want to define a @:to function for Shape (or something) so that it automatically converts to AngledRectangle when requested, as I have a Type.createInstance() call that accepts AngledRectangles and not shapes:
var obj:Dynamic = Type.createInstance(type, [cast(shape, AngledRectangle)]);
Shape is rather simple, it looks like this (also has fields x, y, and angle):
class Shape extends TransformablePropertyElementBase {
public var type(default, null):String;
public var width(default, null):Float;
public var height(default, null):Float;
public function new(shapeNode:Fast) {
super(shapeNode);
type = shapeNode.att.type;
width = Std.parseFloat(shapeNode.att.width);
height = Std.parseFloat(shapeNode.att.height);
}
}
And AngledRectangle is equally simple:
class AngledRectangle extends Rectangle {
public var angle:Float;
public function new(x:Float=0, y:Float=0, width:Float=0, height:Float=0, angle:Float=0) {
super(x, y, width, height);
this.angle = angle;
}
}
I've tried creating an abstract that wraps this like so, but it doesn't seem to do what I want...
abstract AbstractShape(Shape) {
public inline function new(fast:Fast) {
this = new Shape(fast);
}
@:to public inline function toRectangle():Rectangle {
return new Rectangle(this.x, this.y, this.width, this.height);
}
@:to public inline function toAngledRectangle():AngledRectangle {
return new AngledRectangle(
this.x, this.y, this.width, this.height, this.angle);
}
}
But I still get the error, despite changing all my calls to new AbstractShape():
[Fault] exception, information=TypeError: Error #1034: Type Coercion failed: cannot convert centzon.hell.loader::Shape@6985ea1 to centzon.common.openfl.AngledRectangle.
What am I doing wrong/misunderstanding?
-Zack