OBSOLETE: This library is no longer maintained
This library enables overloading of unary and binary haxe operators. It can be used to allow mathematical operations on complex data structures, or give a whole new meaning to the array access operator [] or the new
operator.
Working with this library consists of two steps:
YourOperatorClass
defining the operators andhxop.Overload<YourOperatorClass>
where you want to use itBundled with this library are a few operator-defining classes, which include
Int32
and Int64
: use haxe.Int32 and haxe.Int64 as if they were normal IntsPoint
: support for nme's Point data structureComplex
: use concise infix notation on complex numbersQuaternion
: if you want to rotate things in funny ways, this math is for youReflection
: translate o["field"] to matching Reflect callsYou create your own operators by defining a class with static fields annotated by @op("operator"). As a non-mathematical example, assume that you have a class Signal that dispatches events to registered listeners. You like C#'s += operator, so you want to mimic this in haxe:
class SignalMath
{
@op("+=") static public function add(lhs:Signal, rhs:Void->Void)
{
lhs.add(rhs);
return lhs;
}
}
With just that you can start using your += operator like so:
class Main implements hxop.Overload<SignalMath>
{
static public function main()
{
var signal = new Signal();
s1 += function()
{
trace("I was called today.");
}
s1.dispatch();
}
}
@op
accepts a second argument of type Bool
, which defaults to false and defines if an operator is commutative. For example, if you define an operator + that adds Float
and Point
, you likely want to allow this on both (Float + Point)
and (Point + Float)
, so you would set this argument to true. Note that this only makes sense if your operands are of different types.hxop.Overload
, you can annotate it with @noOverload
.@noAssign
metadata.new
is supported as a pseudo-operator.