HaxeFoundation / HaxeFoundation/haxe

bound method "propagation"

Open
#9,268 6 comments 0 reactions 2 assignees Claimed by @Simn View on GitHub
enhancement feature-analyzer waiting-for-feedback
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

Suppose we have this code:
```haxe
class Future {
function new() {}
function trigger() {
trace("triggered");
}
public static inline function async(executor:(cb:()->Void)->Void):Future {
var future = new Future();
executor(future.trigger);
return future;
}
}

class Main {
static function main() {
var f = Future.async(cb -> doAsyncThings(() -> cb()));
trace(f);
}

static function doAsyncThings(f:()->Void) f();
}
```

this will currently generate:

```js
Main.main = function() {
var future = new Future();
var cb = $bind(future,future.trigger);
Main.doAsyncThings(function() {
cb();
});
console.log("src/Main.hx:16:",future);
};
```

which is technically correct, however it's not the best performane-wise, because we don't really need the `$bind` here. Ideally, we would tempvar the object and "propagate" method call, i.e.:

```js
Main.main = function() {
var future = new Future();
Main.doAsyncThings(function() {
future.trigger();
});
console.log("src/Main.hx:16:",future);
};
```
(not tempvaring `future` here as it's already a local)

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.