Can we fix this?
jfondren
julian.fondren at gmail.com
Thu Sep 30 02:25:53 UTC 2021
On Thursday, 30 September 2021 at 02:03:39 UTC, Basile B. wrote:
> On Thursday, 30 September 2021 at 00:59:26 UTC, deadalnix wrote:
>> [...]
>> Now, this is expected isn't it? There is one and only one i
>> variable. But let's change things a bit.
>>
>> ```d
>> int i = 1;
>> while (i < 4) {
>> int n = i + 2;
>> dgList ~= { writeln(n); }; // still outputs 5 5 5
>> }
>> ```
>>
>> Now this is wrong.
>
> Why is this wrong ? Do you expect one alloca per iteration for
> n ?
No, what I expect is for the closure to close over the n, and to
do whatever it needs to do for that to work. In basically every
language that exists the expected behavior from this code is 3 4
5. Doing it otherwise is like PHP getting the ternary operator
backwards -- it's not different because it's innovating, it's
different because it's wrong.
More information about the Digitalmars-d
mailing list