Simn-haxe2 / hxop

Operator overloading for haxe.
15 stars 0 forks source link

Haxe operator overloading

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.

Usage

Working with this library consists of two steps:

  1. Create a class YourOperatorClass defining the operators and
  2. add implements hxop.Overload<YourOperatorClass> where you want to use it

Bundled with this library are a few operator-defining classes, which include

Defining and using operators

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:


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();
    }
}

Remarks