This one weird trick allows you to capture loop variables.

Sebastiaan Koppe mail at skoppe.eu
Sun Oct 20 18:14:59 UTC 2024


On Sunday, 20 October 2024 at 17:48:15 UTC, Paul Backus wrote:
> This is cute and all, but the correct solution is to fix the 
> damn compiler. The fact that this is necessary in the first 
> place is an embarrassment.

When I first encountered closures in D and saw how easily they 
capture surrounding variables it felt like magic.

Nowadays I dislike them because they allocate behind the scene, 
and you still get this odd foreach behavior mentioned here - 
which, to be fair, however unintuitive, I believe to be correct 
given the interplay of features.

The only good solution I see is to get rid of implicit captures 
and instead require explicit declaration of what and _how_ to 
capture, i.e. by ref or by value.


More information about the Digitalmars-d mailing list