HaxeFoundation / HaxeFoundation/haxe

[js] type inference breaks calls due to missing $bind

Open
#8,414 7 comments 0 reactions 0 assignees View on GitHub
bug feature-type-inference platform-javascript
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

```haxe
class Main {
static function main() {
foo(new Main());
}

static function foo(main) {
bar(main.call);
}

static function bar(f:() -> Int) {
trace(f());
}

function new() {}

var field = 0;

public function call():Int {
return field;
}
}
```

Crashes with:

```
return this.field;
^

TypeError: Cannot read property 'field' of undefined
```

With the above `foo` looks like this:

```haxe
Main.foo = function(main) {
Main.bar(main.call);
};
```

Explicitly type-hinting `main` in `foo` as `main:Main` works since a `$bind` ends up being generated:

```haxe
Main.foo = function(main) {
Main.bar($bind(main,main.call));
};
```

It also works with `main:{function call():Int;}`, which is why @nadako mentioned this is related to some previous discussions about whether unification between functions and function-typed vars in structures should be allowed.

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.