HaxeFoundation / HaxeFoundation/haxe

Overloads can't discriminate function types with optional arguments

Open
#10,611 4 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

This doesn't work:
https://try.haxe.org/#c35B8161
```haxe
class Test {
static function main() {
var foo:?Int->Void = (?foo:Int) -> trace(foo);
var bar:?Int->Void = test(foo); // Ambiguous overload
}

extern inline overload static function test(f:?Int->Void):?Int->Void return f;
extern inline overload static function test(f:Int->Void):Int->Void return f;
}
```

But this doesn't work either (using `?SomeType` as `T`):
https://try.haxe.org/#EB5c76aE
```haxe
class Test {
static function main() {
var foo:?Int->Void = (?foo:Int) -> trace(foo);
var bar:?Int->Void = test(foo); // error: Optional parameters can't be forced
}

static function test(f:T->Void):T->Void return f;
}
```

Also note that `@:overload` was able to differentiate optional arguments, but if you need a different implementation for them it won't help much:
https://try.haxe.org/#12f271C3
```haxe
class Test {
static function main() {
var foo:?Int->Void = (?foo:Int) -> trace(foo);
var bar:?Int->Void = test(foo);
bar(42); // ok

var foo:Int->Void = (foo:Int) -> trace(foo);
var bar:Int->Void = test(foo);
bar(42); // ok too
}

// Had to use the optional arguments one as main type to avoid the "Optional parameters can't be forced" error
@:overload(function(f:Int->Void):Int->Void {})
static function test(f:?Int->Void):?Int->Void return f;
}
```

Contributor guide

Open the contributing guide

Research direction

Start with the two linked Try Haxe examples and compare their overload-resolution behavior with the @:overload example in the issue. Trace the compiler path that handles optional function arguments and overload selection, then verify that the examples no longer produce the reported ambiguity or optional-parameter error while the demonstrated @:overload behavior remains valid.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.