Skip to content

Failed in extending the operator in subclass #4

@yiakwy

Description

@yiakwy

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions