Automatically converting between types

51 views
Skip to first unread message

Zachary Murray

unread,
Jan 21, 2014, 12:51:47 PM1/21/14
to haxe...@googlegroups.com
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

Juraj Kirchheim

unread,
Jan 21, 2014, 1:42:01 PM1/21/14
to haxe...@googlegroups.com
There's a difference between a cast and an implicit conversion. The
former happens at runtime, the latter at compiletime, being purely a
Haxe feature that the AVM2 has no knowledge of.
This will work:

var shape:AbstractShape = ...;
var rect:AngledRectangle = shape;

Try compiling with -D dump=pretty and then you will have a more less
human readable representation of what's actually going on under the
hood (including how abstracts are handled at runtime).

Regards,
Juraj
> --
> To post to this group haxe...@googlegroups.com
> http://groups.google.com/group/haxelang?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Haxe" group.
> For more options, visit https://groups.google.com/groups/opt_out.

Zachary Murray

unread,
Jan 21, 2014, 5:00:15 PM1/21/14
to haxe...@googlegroups.com
Thanks, Juraj. That totally worked. :) It makes sense, although I think conventional wisdom (which I admit doesn't always apply to software) would lead me to believe that a cast would be more of an "explicit" conversion. I get now though that they are very different things.

Follow-up question though: how does @:forward work, exactly? I basically can't find any documentation on it, but it seems like exactly what I want to do for my AbstractShape, as Shape has a ton of fields that I want to access. I wrote a bunch of getters, but... @:forward just seems so much nicer. It doesn't seem to do anything for me though (on Haxe 3.0.1).


You received this message because you are subscribed to a topic in the Google Groups "Haxe" group.

Juraj Kirchheim

unread,
Jan 23, 2014, 4:01:40 AM1/23/14
to haxe...@googlegroups.com
On Tue, Jan 21, 2014 at 11:00 PM, Zachary Murray
<dremel...@gmail.com> wrote:
> Thanks, Juraj. That totally worked. :) It makes sense, although I think
> conventional wisdom (which I admit doesn't always apply to software) would
> lead me to believe that a cast would be more of an "explicit" conversion. I
> get now though that they are very different things.

You only ever do an explicit cast when you know something the compiler doesn't.

> Follow-up question though: how does @:forward work, exactly? I basically
> can't find any documentation on it, but it seems like exactly what I want to
> do for my AbstractShape, as Shape has a ton of fields that I want to access.
> I wrote a bunch of getters, but... @:forward just seems so much nicer. It
> doesn't seem to do anything for me though (on Haxe 3.0.1).

You will need a nightly build for that. Truth be told, I haven't tried
yet, so maybe someone else can comment on the specifics.

Regards,
Juraj
Reply all
Reply to author
Forward
0 new messages