Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:26:59 UTC, kdevel wrote: What is the lifetime of the first loop's variable i? It lives as long as the delegate. What about this example: ``` bug2.d import std.stdio; void main () { int delegate () [] dg; foreach (i; 0..2) { int *j; if (i

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread kdevel via Digitalmars-d-learn
On Thursday, 29 March 2018 at 20:05:35 UTC, ag0aep6g wrote: On Thursday, 29 March 2018 at 19:02:51 UTC, kdevel wrote: On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: [...] int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2)

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On Thursday, 29 March 2018 at 19:02:51 UTC, kdevel wrote: On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: [...] int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2) writeln (guns[i] ()); // 1 and 1, why? Isn't this undefined

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread kdevel via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: import std.stdio; void main () { int delegate () [] funs; funs ~= () => 0; funs ~= () => 1; foreach (i; 0..2) writeln (funs[i] ()); // 0 and 1 as expected int delegate () [] guns;

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread Ivan Kazmenko via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:38:14 UTC, ag0aep6g wrote: <...> With immutable, this is certainly a problem. https://issues.dlang.org/show_bug.cgi?id=2043 Wow, such history for the bug! Two possible workarounds: int delegate () [] iuns; foreach (i; 0..2) iuns ~= (j) { return () =>

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread Dennis via Digitalmars-d-learn
On Thursday, 29 March 2018 at 15:16:07 UTC, Ivan Kazmenko wrote: So, why do delegates of guns[] and huns[] all return 1, and how to correctly reproduce the behavior of funs[] while populating it in a loop? A delegate is a function with a pointer to the stack frame where it was created. It

Re: how to correctly populate an array of dynamic closures?

2018-03-29 Thread ag0aep6g via Digitalmars-d-learn
On 03/29/2018 05:16 PM, Ivan Kazmenko wrote: int delegate () [] guns; foreach (i; 0..2) guns ~= () => i; foreach (i; 0..2) writeln (guns[i] ());  // 1 and 1, why? Because there's only variable `i`. All delegates refer to that same one. With `i` being mutable, this could maybe

how to correctly populate an array of dynamic closures?

2018-03-29 Thread Ivan Kazmenko via Digitalmars-d-learn
Here's a simplified example of what I want to achieve. I first create funs, an array of two delegates. I want funs[0] to always return 0 and funs[1] to always return 1. By assigning the constants directly (see the code below), I achieve exactly that. Now, I want to use a loop to assign the