HaxeFoundation / HaxeFoundation/haxe

`overload` weaker than `@:overload` wrt type inference

Open
#10,263 4 comments 0 reactions 1 assignee Claimed by @Simn View on GitHub
bug feature-overload
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

While trying to find the overload we want, compiler will get errors on "bad" overloads, but won't discard them when it finds a perfect one.

Example below can be made to break consistently by changing `@:overload` order, but that's not the point; `@:overload` _can_ be made to work with good ordering, while `overload` will always fail if one overload errors [because of type inference].

```haxe
class Test {
static function main() {
// Works fine with `@:overload`
setState2(state -> {
state.foo.set("foo", 1);
state.foo.set("foo", '1'); // This is fine
return state;
});

// Doesn't work with `overload` because errors are not discarded
setState(state -> {
$type(state.foo); // Twice Warning : Unknown<0>, once Warning : Map
state.foo.set("foo", 1);
state.foo.set("foo", '1'); // String should be Int
return state;
});
}

extern inline overload static function setState(v:State, ?cb:Void->Void):Void {}
extern inline overload static function setState(v:State->State, ?cb:Void->Void):Void {}
extern inline overload static function setState(v:State->Props->State, ?cb:Void->Void):Void {}

@:overload(function(v:State, ?cb:Void->Void):Void {})
@:overload(function(v:State->Props->State, ?cb:Void->Void):Void {})
static function setState2(v:State->State, ?cb:Void->Void):Void {}
}

typedef State = {
var foo:Map;
}

typedef Props = {}
```
https://try.haxe.org/#0415D44A

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.