Can we fix this?

Steven Schveighoffer schveiguy at gmail.com
Thu Sep 30 14:42:34 UTC 2021


On 9/30/21 9:55 AM, deadalnix wrote:
> On Thursday, 30 September 2021 at 11:36:01 UTC, bauss wrote:
>> On Thursday, 30 September 2021 at 11:32:28 UTC, Sebastiaan Koppe wrote:
>>> On Thursday, 30 September 2021 at 11:00:15 UTC, deadalnix wrote:
>>>> D either needs to ditch constructor, destruction, immutable, and 
>>>> anything that has to do with lifetime.
>>>
>>> I rather have closures that require you to state what you capture.
>>
>> I really like that in C++ because you don't clutter memory with 
>> unnecessary references. Your closure will only reference what you 
>> capture.
> 
> I have good news for you: the compiler knows what you capture and what 
> you don't, so the only case where in which you'll have unnecessary 
> reference, is if you capture explicitly and mess it up.

You may want to capture variables that would normally be referenced. The 
compiler can't possibly know what you want exactly.

Consider:

```d
void delegate() dg;
for(int i = 0; i < 100; ++i)
{
    dg = { writeln(i); };
}

dg();
```

What `i` is printed? Note that there is only one `i` variable over the 
entire loop.

-Steve


More information about the Digitalmars-d mailing list