D isn't the only language with janky closure semantics

Steven Schveighoffer schveiguy at gmail.com
Fri Aug 29 22:51:10 UTC 2025


On Friday, 29 August 2025 at 22:28:17 UTC, H. S. Teoh wrote:
> Now, JS is certainly not a model language to follow, but it's 
> interesting how it shares with D the same issue over closures 
> involving loop variables.
>
> I guess that means we're not alone. :-P  Even though this 
> situation is definitely not ideal.

Oh yeah, JS has similar behavior. You get the desired behavior by 
using `let` at the variable declaration. Otherwise, it becomes a 
regular declaration (one instance per function).

I think your original code will work as you want with:

```js
for (let button of buttons) {
```

Would be nice to have a similar trick for D...

-Steve


More information about the Digitalmars-d mailing list