7 ms·
I don't think it's fair to call Go and Javascript's behavior "implicit", they just always capture variables by reference. Rust variable capture is implicit tho
by sapiogram 1y ago
I don't think it's fair to call Go and Javascript's behavior "implicit", they just always capture variables by reference.
Rust variable capture is implicit though, but it can't cause the problems described in the article, since mutable references are required to be unique.
- _old_dude_ 1y agoIn JavaScript, a 'let' inside the initializer of a for loop is captured by value, all the others are captured by reference. I think it's fair to call that semantics "implicit".
- sapiogram 1y agoNo, that's a mistake in the article. The variable is still captured by reference, but `let` is causing it to be re-declared on every iteration of the loop, not mutated. The following code prints 1, 2, 3. It wouldn't do that if the variable was captured by value. for (let i = 0; i < 3;) { setTimeout(() => console.log(i)); i++; }
- anonymoushn 1y agoThe behavior of "let" with for loops where the variable is declared more times than it is initialized, despite the source code having one declaration that is also the only initialization, is not very explicit.
- sapiogram 1y agoFair, but that's a property of for loops. Variable in closures are still always captured by reference.
- Lerc 1y agoconsider this for (let i=0;i<3;i++) { i+=10; setTimeout(_=>console.log(i),30); i-=10 } Capture by value would print 10, 11, 12 that's the value when it was captured Capture by reference would print 0,1,2 It's much easier to conceptualise it as for (const i=0;i<3;i++) { setTimeout(_=>console.log(i),30); } which is fine because i never changes. It is a different i each time. fancier example for (let x = 0, y = 0; y < 2; x=x++<3?x:y++,0){ x+=10; y+=10; console.log("inline:",x,y); setTimeout(_=>console.log("timeout",x,y),30); x-=10; y-=10; }
- IshKebab 1y ago> which is fine because i never changes. It is a different i each time. This is clearly a super weird hack to make closure capture behave more like you'd want. There's a reason this level of weirdness isn't needed in C++.