https://issues.dlang.org/show_bug.cgi?id=21929
--- Comment #13 from deadalnix <[email protected]> --- (In reply to Walter Bright from comment #10) > Replying to comment #1: > > The observed behavior is a direct result of lambdas in D not capturing by > value, they capture by reference. > This is true of the first exemple, for which things behave apropriately. This is not true of the second one, as, even though it capture by reference, it capture a reference to a new variable every time. > Even allocating a closure on the gc heap is not a solution here, because only > one allocation will be made which will be shared by each lambda, exhibiting > the same behavior. What's nice when most options are incorrect, is that it's fairly obvious what the correct ones are. in this cases, there are 3: - All variables have function scope, like in python. - A new closure is allocated for every loop iteration, like in JavaScript or C#. - Don't pretend you are a safe language in any way like C++ and put the burden of getting things right on the user. Considering D as it stands, it is obvious that the second choice is the correct one. But can also chose to do away with RAII and go for the first option. Or we can get rid of @safe . Or we can ignore the issue for one more decade and hope it goes away as everybody moves to rust. --
