foxbenjaminfox / babel-plugin-overload

A highly experimental babel plugin for operator overloading in javascript
Apache License 2.0
46 stars 6 forks source link

Failed in extending the operator in subclass #4

Open yiakwy opened 6 years ago

yiakwy commented 6 years ago

Hi, I am going to extend operators in the following manner:

export class Operator {

    __add__(other: VecInterface) {
        throw Error("__add__ not implemented yet!")
    }

    // $FlowFixType
    [Symbol.for('+')] (other: any) {
        return this.__add__(other)
    }

    __sub__(other: VecInterface) {
        throw Error("__sub__ not implemented yet!")
    }

    // $FlowFixType
    [Symbol.for('-')] (other: any) {
        return this.__sub__(other)
    }

}

// I use mixin because Point2D should inherit it from Geometry, and es6 is currently only supporting mono inheritance
mixin(Point2D.prototype, Operator.prototype) // like jquery `extend`

I tested it in runtime by executing

let a = new Point2D(1, 0)
let b = new Point2D(0, 1)
let c = a + b // VecInterface point to Vec2 or Vec3

But it add, and sub was not fired. Could you give me any suggestion about it?