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:
- Create a class
YourOperatorClass
defining the operators and - add implements
hxop.Overload<YourOperatorClass>
where you want to use it
Bundled with this library are a few operator-defining classes, which include
Int32
andInt64
: 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 calls
You 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:
- SignalMath.hx
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:
- Main.hx
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 typeBool
, which defaults to false and defines if an operator is commutative. For example, if you define an operator + that addsFloat
andPoint
, 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.- If you have a situation where you want to disable overloading for a specific function of a class implementing
hxop.Overload
, you can annotate it with@noOverload
. - Usually, assignment operators such as += and *= generate an assignment of their return value to the left hand side argument. If you wish to disable it for certain operators, you can add the
@noAssign
metadata. - Supported operators are all unary and binary haxe operators. Also, array access [] is treated as a binary operator lhs[rhs] and
new
is supported as a pseudo-operator.