Let's get the semantic around closure fixed.
Walter Bright
newshound2 at digitalmars.com
Wed May 19 03:01:24 UTC 2021
On 5/18/2021 9:47 AM, deadalnix wrote:
> Long story short: https://issues.dlang.org/show_bug.cgi?id=21929
>
> Closure do not respect scope the way they should. Let's fix it.
Let's rewrite it to something that does not use closures:
int test() @safe {
int j;
int*[20] ps;
for (int i = 0; i < 10; i++) {
ps[j++] = &i;
}
for (int i = 0; i < 10; i++) {
int index = i;
ps[j++] = &index;
}
int x;
foreach (p; ps) {
x += *p;
}
return x;
}
This code is equivalent in terms of what is happening with references and scopes.
Compiling it with -dip1000 yields:
Error: address of variable i assigned to ps with longer lifetime
Error: address of variable index assigned to ps with longer lifetime
Which is pragmatically what the behavior of the delegate example would be,
because the delegate is also storing a pointer to the variable.
More information about the Digitalmars-d
mailing list