I want to make a Vector2 Class which would allow me to use mathematical operators instead of having to call .add() .subtract() and so on. It’s not a life or death matter, but it’d be nice to get it to work.

So far I have discovered the Proxy Class and attempted to accomplish the task by overriding the call property method. This has lead me to the following:

a + b

If both a and b are strings, concat.

If one of a or b is a string, toString() the other, concat.

If both a and b are Number/int/uint, add mathematically.

Otherwise, valueOf() each that is not a number or string, toString() each that is still not a number or string and follow the previous rules.

This poses a problem that if anything other than a number (or int/uint) is present, the operation defaults to string concatenation. And I can’t really express a Vector2 as a single number, so no matter what I do, I end up with string concatenation. While I can change the valueOf() and toString() methods, I have no way (that I know of) to control the the + operator at all.

So, is there any way to solve this problem without going completely dodgy?