For a Java library containing the following (compressed) class:
public class Vector2f {
public float x;
public float y;
public Vector2f div(Vector2f other) {
x /= other.x;
y /= other.y;
return this;
}
public Vector2f div(Vector2f other, Vector2f dest) {
dest.x = x / other.x;
dest.y = y / other.y;
return dest;
}
}
Since kotlin automatically converts suitable method names to overloaded operators, I can write
val v0 = Vector2f(12f, 12f)
val v1 = Vector2f(2f, 2f)
val res = v0 / v1
println(v0)
println(v1)
println(res)
res.x = 44f
println()
println(v0)
println(v1)
println(res)
... with a completely unexpected result that v0gets the mutation using the infix division operation. In addition, the link stored in respoints to the same object as v0Exit:
Vector2f(6.0, 6.0)
Vector2f(2.0, 2.0)
Vector2f(6.0, 6.0)
Vector2f(44.0, 6.0)
Vector2f(2.0, 2.0)
Vector2f(44.0, 6.0)
Since the library also provides an overload for writing the result to another vector, I was wondering if I could say “kotlin” so as not to use the provided method Vector2f.div(Vector2f).
I already tried to provide an extension method Vector2f, but which is obscured by the real member:
operator fun Vector2f.div(other: Vector2f): Vector2f = this.div(other, Vector2f())
^~~ extension is shadowed by a member: public open operator fun div(other: Vector2f!): Vector2f!