Why is D unpopular, redux.
Timon Gehr
timon.gehr at gmx.ch
Tue May 24 07:45:38 UTC 2022
On 24.05.22 07:31, Walter Bright wrote:
>
>> https://issues.dlang.org/show_bug.cgi?id=2043
>
> We have better tools to deal with 2043 today. Note that dgList exceeds
> the lifetime of b, and should fail to compile for that reason.
>
> Let's try it (adding import and @safe and updating the code to D2):
> ----
> import std.stdio;
>
> @safe:
>
> void delegate()[] dgList;
>
> void test() {
>
> foreach(int i; [1, 2, 3]) {
> int b = 2;
> dgList ~= { writeln(b); };
> writeln(&b); // b will be the *same* on each iteration
> }
> }
> ----
>
> > dmd test.c -preview=dip1000
> test.d(13): Error: reference to local variable `b` assigned to non-scope
> parameter `_param_0`
>
> Looks like we can close it now!
No. The correct behavior when a delegate literal outlives captured
variables is to allocate a closure. This case is not an exception...
More information about the Digitalmars-d
mailing list