So let's get this forum rolling.
This question already popped up in the initial JSFML discussion: how will overloaded operators (e.g. for sf::Vector or sf::Color) be represented in JSFML?
I have worked with several Java libraries that attempted to do this, and every single one of them had some kind of ugly flaw to it that made working with it a pain. The approach I personally regard as the "best" is to interpret operators as static functions within the class in question. Unary operators take one parameter, binary operators take two parameters. The returned object is actually a new one, so none of the objects passed is altered in any way.
So, as an example, the + operator for Vector2f would look like this:
class Vector2f {
public static Vector2f add(Vector2f a, Vector2f b) {
return new Vector2f(a.x + b.x, a.y + b.y);
}
[...]
}
An alternative to calling it "add" would be "sum", but personally I see it like functions should be named after what they do, not after what they yield.
Now, assignment operators like += that alter the left operand would be missing. They certainly are very convenient, plus you don't always want the result to be in a new object. I would implement them as object methods like this:
class Vector2f {
public Vector2f add(Vector2f v) {
this.x += v.x;
this.y += v.y;
return this;
}
}
I am not sure whether it's a problem to name them the same. From the context (object or class), it should be easy enough to differentiate. The += should yield "this" in my opinion, as this allows chain calls such as:
Vector2f v = new Vector2f(1, 0).add(u).mul(4).normalize();
So, in conclusion, I consider the following methods for Vector2f:
static (returns new object):
static Vector2f add(Vector2f a, Vector2f b); //vector addition
static Vector2f sub(Vector2f a, Vector2f b); //vector subtraction
static Vector2f mul(Vector2f a, float s); //scalar multiplication
static Vector2f mul(Vector2f a, Vector2f b); //scalar (component-wise) multiplication
static float dot(Vector2f a, Vector2f b) //dot product
static Vector2f normal(Vector2f v); //normal of v
static Vector2f neg(Vector2f v); //negated v
object (returns this):
Vector2f add(Vector2f other); //vector addition
Vector2f sub(Vector2f other); //vector subtraction
Vector2f mul(float s); //scalar multiplication
Vector2f mul(Vector2f other); //scalar (component-wise) multiplication
Vector2f normalize(); //normalizes the vector
Vector2f negate(); //negates the vector
Classes such as Color or Time would get their respective versions.