HaxeFoundation / HaxeFoundation/haxe

unnecessary wrapping of captured vars in loop

Open
#9,625 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

*extracted from #9624*

I'm pretty sure we have an issue about this somewhere, but i couldn't find it:

```haxe
function main() {
var x = 0;
var i = 0;
while (i < 5) {
var j = x;
function capture() {
trace(j);
}
capture();
}
}
```

Generates the following ES5 (but NOT ES6):
```js
function Main_main() {
var x = 0;
var i = 0;
while(i < 5) {
var j = [x];
var capture = (function(j) {
return function() {
console.log("src/Main.hx:7:",j[0]);
};
})(j);
capture();
}
}
```

Changing `while` to `if` removes the wrapping, so it's specific to loops, it seems. Also it's not JS-specific, result is the same also for Flash and Python, but not for C# and C++, so this seems dependant on platform settings.

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.