On Friday, 4 August 2017 at 17:27:52 UTC, Timon Gehr wrote:
On 04.08.2017 18:57, bitwise wrote:
[...]
3) heap allocate a chunk of space for each captured scope (as
in lisp and C#).
The way to go is 3). 1) is bad, because it completely prevents
closures from being escaped, 2) is bad because it does not
allow sharing of closure memory.
Makes sense.
When I look at the output I get from the code above though, it
seems like neither of these things were done, and that someone
has gone way out of their way to implement some very strange
behavior.
...
Absolutely not. The current behavior was quite straightforward
to implement, but it is wrong. Bugs often lead to strange
behavior. This does not imply that such bugs are intentional.
In hindsight, I would have to agree that the current approach may
be a little _too_ straight forward ;)
[...]
You can work around the bug like this:
foreach(i;0..5)(){
int j=i;
funs~=(){ writeln(j); };
}()
Thanks for this - most workarounds I came across this morning
were pretty bloated.