HaxeFoundation / HaxeFoundation/haxe

operator overloading – wrong function used (definition order-dependent)

Open
#9,980 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
var pos = new Vec2(1, 1);
var t = pos;
pos *= 10; // Will call `mul` and will create a new instance.
assert(t == pos); // Fail.

@:forward abstract Vec2({x:Float, y:Float}) from {x:Float, y:Float} to {x:Float, y:Float} {
public inline function new(x:Float, y:Float) this = { x: x, y: y };
@:op(A * B) public inline function mul(f:Float) return new Vec2(this.x * f, this.y * f);
@:op(A *= B) public inline function applyMul(f:Float) {
this.x *= f;
this.y *= f;
return this;
}
}
```
If the `applyMul` function is declared _before_ `mul`, everything works as expected. Reproduced on 4.1, 3.4, and nightly.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.