HaxeFoundation / HaxeFoundation/haxe
[type system] type inference vs call return types vs abstract casts
- Dominant language
- Haxe
- Stars
- 6.9k
- Forks
- 715
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 11
Description
I've got a rather interesting case with abstract casts that resulted into a run-time error, not quite sure if this can be considered a type hole or not... The problem is that the `link` monomorph is bound to a function instead of `CallbackLink` because the `bind` arguments are processed before its return type. And then the `toFunction` abstract cast is inserted to unify resulting `CallbackLink` to that fucntion type.
```haxe
abstract CallbackLink(()->Void) {
public function cancel() {
if (this != null) this();
}
@:to function toFunction():()->Void {
return if (this == null) noop else this;
}
static function noop() {}
}
function bind(f:()->Void):CallbackLink {
f();
return null;
}
function main() {
var link = null; // mono
link = bind(() -> {
link(); // makes `link` a `()->Void`
// runtime error here because `link` is not `CallbackLink`,
// expected a compile-time error (`CallbackLink` is not callable)
});
$type(link); // () -> Void, not CallbackLink
// `toFunction` cast added for `link`
}
```
Generated js:
```js
function Main_main() {
let link = null;
link = CallbackLink.toFunction(Main_bind(function() {
link();
}));
}
```
Contributor guide
Assessment
This issue has not been assessed yet.