On Thursday, 30 September 2021 at 12:55:48 UTC, Max Samukha wrote:
>Yes, that is the semantics I would expect. Note the complication when vars from multiple scopes are captured:
int i = 1;
dgList ~= { writeln(i); };
while (i < 4) {
immutable int n = i++;
dgList ~= { writeln(i, " ", n); };
}
Expected:
4
4 1
4 2
4 3
That would require an implementation like this:
static struct __C0
{
int i;
void call() { writeln(i); }
}
auto c0 = new __C0(1);
dgList ~= &c0.call;
while (c0.i < 4) {
static struct __C1 {
__C0* c0;
immutable int n;
void call() { writeln(c0.i, " ", n); }
}
dgList ~= &(new __C1(c0, c0.i++)).call;
}
That is indeed the most expected behavior that is consistent with D's semantic in other areas of the language - notably construction/destruction and immutability.