Can we fix this?

deadalnix deadalnix at gmail.com
Thu Sep 30 10:56:32 UTC 2021


On Thursday, 30 September 2021 at 02:25:53 UTC, jfondren wrote:
> 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.

It is worse than this.

PHP getting the ternary operator backward is unfortunate, error 
prone and all, but all in all, it is consistent.

This is inconsistent, which is much more serious problem, as it 
means the language is unable to provide the invariants its 
constructs are supposed to provide.


More information about the Digitalmars-d mailing list