Re: Strange closure behaviour

2019-06-16 Thread Rémy Mouëza via Digitalmars-d-learn
On Sunday, 16 June 2019 at 01:36:38 UTC, Timon Gehr wrote: It's a bug. It's memory corruption. Different objects with overlapping lifetimes use the same memory location. Okay. Seen that way, it is clear to me why it's a bug. ... No, it's not the same. Python has no sensible notion of

Re: Strange closure behaviour

2019-06-15 Thread Timon Gehr via Digitalmars-d-learn
On 15.06.19 18:29, Rémy Mouëza wrote: On Saturday, 15 June 2019 at 01:21:46 UTC, Emmanuelle wrote: On Saturday, 15 June 2019 at 00:30:43 UTC, Adam D. Ruppe wrote: On Saturday, 15 June 2019 at 00:24:52 UTC, Emmanuelle wrote: Is it a compiler bug? Yup, a very longstanding bug. You can work

Re: Strange closure behaviour

2019-06-15 Thread Emmanuelle via Digitalmars-d-learn
On Saturday, 15 June 2019 at 16:29:29 UTC, Rémy Mouëza wrote: I don't know if we can tell this is a compiler bug. The same behavior happens in Python. The logic being variable `x` is captured by the closure. That closure's context will contain a pointer/reference to x. Whenever x is updated

Re: Strange closure behaviour

2019-06-15 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 15 June 2019 at 16:29:29 UTC, Rémy Mouëza wrote: I don't know if we can tell this is a compiler bug. I can't remember where the key fact was, but I used to agree with you (several languages work this same way, and it makes a lot of sense for ease of the implementation), but

Re: Strange closure behaviour

2019-06-15 Thread Rémy Mouëza via Digitalmars-d-learn
On Saturday, 15 June 2019 at 01:21:46 UTC, Emmanuelle wrote: On Saturday, 15 June 2019 at 00:30:43 UTC, Adam D. Ruppe wrote: On Saturday, 15 June 2019 at 00:24:52 UTC, Emmanuelle wrote: Is it a compiler bug? Yup, a very longstanding bug. You can work around it by wrapping it all in another

Re: Strange closure behaviour

2019-06-14 Thread Emmanuelle via Digitalmars-d-learn
On Saturday, 15 June 2019 at 00:30:43 UTC, Adam D. Ruppe wrote: On Saturday, 15 June 2019 at 00:24:52 UTC, Emmanuelle wrote: Is it a compiler bug? Yup, a very longstanding bug. You can work around it by wrapping it all in another layer of function which you immediately call (which is fairly

Re: Strange closure behaviour

2019-06-14 Thread Adam D. Ruppe via Digitalmars-d-learn
On Saturday, 15 June 2019 at 00:24:52 UTC, Emmanuelle wrote: Is it a compiler bug? Yup, a very longstanding bug. You can work around it by wrapping it all in another layer of function which you immediately call (which is fairly common in javascript): funcs ~= ((x) => (int i) {

Strange closure behaviour

2019-06-14 Thread Emmanuelle via Digitalmars-d-learn
Take a look at this code: --- import std.stdio; void main() { alias Func = void delegate(int); int[][] nums = new int[][5]; Func[] funcs; foreach (x; 0 .. 5) { funcs ~= (int i) { nums[x] ~= i; }; } foreach (i, func; funcs) { func(cast(int) i); }