A couple of extensions to `with` that would be worthwhile

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Wed Oct 13 19:18:55 UTC 2021


On 2021-10-13 9:20, Paul Backus wrote:
> On Wednesday, 13 October 2021 at 13:16:36 UTC, Basile B. wrote:
>> I think that the point of proposal 1 is to have something like, after 
>> lowering to common AST constructs:
>>
>> ```d
>> {
>>   {
>>     auto xpath = ....;
>>     scope (exit) destroy(xpath)
>>     // stuff
>>   }
>>   // destroy(xpath) really happens here
>> }
>> ```
> 
> Destructors are already called automatically at the end of a block scope.

The point is that with keeps the entire expression live, even if you 
only care about a part of it:

struct S { int x; ... }

with (auto y = S().x)
{
    ...
}

In this case the S() temporary will be destroyed only at the end of 
with's scope.

This enables idioms such as "I want to create a temporary string and 
mess with it".


More information about the Digitalmars-d mailing list