Skip to content

Operator Overloading support

Mr zhan edited this page Jan 12, 2015 · 1 revision

before: you have to add untyped to tell compiler when it need build

var v :Vector3 = new Vector3(1,1,1);
   
   
   
	
	var b=untyped(v+ new Vector3(2,2,2));

	trace(b);

now

  var v :MyVector3 = new Vector3(1,1,1);
    v++;
    trace(v); // 1.5
   
   
	
	var b=v+ new Vector3(2,2,2);

	trace(b);

abstract MyVector3(Vector3) from Vector3 to Vector3 {
    public inline function new(v:Vector3) {
        this = v;
    }

    @:op(A++) public inline function inc() {
        return this = Vector3Methods.add(this, Vector3.one);
    }
	 @:op(A+B) public inline function add(b:Vector3) {
        return  Vector3Methods.add(this, b);
    }
	
}

class Vector3Methods
{
@:keep  public static function add(a:Vector3, b:Vector3) : Vector3 {
    return untyped __lua__("a+b");
  }
 }
Clone this wiki locally