Delegates and values captured inside loops

Steven Schveighoffer schveiguy at gmail.com
Sun Jan 21 19:18:07 UTC 2024


On Sunday, 21 January 2024 at 14:52:45 UTC, Renato wrote:
> On Saturday, 20 January 2024 at 16:53:12 UTC, ryuukk_ wrote:
>>
>> This is the workaround according to: 
>> https://issues.dlang.org/show_bug.cgi?id=21929#c9
>
> Go used to have the same issue [but they fixed 
> it](https://go.dev/blog/loopvar-preview) so this is no longer a 
> problem in Go.
>
> Perhaps D could do something about it for the same reasons the 
> Go blog post presented.

Actually, D is much worse. It appears in that post that local 
variables in the loop were scoped on the loop iteration, but just 
not the iteration variables themselves. This means, the machinery 
to properly capture the loop variables was trivial, just change 
the scope where those variables are allocated.

In D, there is no loop scope. So the compiler would have to 
establish a new mechanism to recognize which variables to stick 
into a closure. It's not impossible, but it is not the same scope 
as what Go had to do.

-Steve


More information about the Digitalmars-d-learn mailing list