Need some help with operators and abstracts

25 views
Skip to first unread message

Marco Salamone

unread,
Jun 27, 2013, 3:53:24 AM6/27/13
to haxe...@googlegroups.com
I'm working on creating a suite of Duple {Int:Int} data types for a discrete 2d cellular environment (point, dimension, vector etc). Mathematically, they share a lot of similar qualities, so I would like to define most of this functionality within an underlying Duple type. I've successfully implemented each of these in the manner that I would like, but I want to refine and minimize the implementation.

I would like all of them to share the same set of overloaded operators and be able to interact with each other freely- however, I would also like to be able to replace a few of these operators for some special applications.

Operator overloading is very type-strict, so even if I make all of my Duple inheritors go "to/from" Duple, none of the operators work even though calling the methods explicitly works fine.

I think that it would be a little obnoxious to have to duplicate the method stubs for every possible arrangement of types for all of my Duple types, so I'd very much like to avoid that, but I'm having trouble rationalizing the best way to do this. I played around with some typedef trickery but couldn't seem to make any progress with that.

I suppose there is some way to use macro reification to produce each of these classes with the entire set of shared fields that I want, but I'm clueless about how I should go down that road.

Anyhow- here's an example of my problem.

abstract Dimension(Duple) from Duple to Duple
{
 
@:op(A * B) static public inline function mul1(l:Dimension,r:Duple);
 
@:op(A * B) static public inline function mul2(l:Dimension,r:Point);
 
@:op(A * B) static public inline function mul3(l:Dimension,r:Vector);
 
@:op(A * B) static public inline function mul4(l:Dimension,r:Dimension);
}
abstract Vector(Duple) from Duple to Duple
{
 
@:op(A * B) static public inline function mul1(l:Vector,r:Duple);
 
@:op(A * B) static public inline function mul2(l:Vector,r:Point);
 
@:op(A * B) static public inline function mul3(l:Vector,r:Vector);
 
@:op(A * B) static public inline function mul4(l:Vector,r:Dimension);
}


abstract Point(Duple) from Duple to Duple
{
 
@:op(A * B) static public inline function mul1(l:Point,r:Duple);
 
@:op(A * B) static public inline function mul2(l:Point,r:Point);
 
@:op(A * B) static public inline function mul3(l:Point,r:Vector);
 
@:op(A * B) static public inline function mul4(l:Point,r:Dimension);
}


abstract Duple({a:Int,b:Int})
{
 
public inline function new(a:Int,b:Int) this = {a:a,b:b};
 
@:op(A * B) static public inline function mul(l:Duple,r:Duple)
 
return new Duple(l.a*r.a,l.b*r.b);
}



Not sure how to proceed, thanks.

Reply all
Reply to author
Forward
0 new messages