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

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


On 2021-10-13 9:05, Adam D Ruppe wrote:
> On Wednesday, 13 October 2021 at 11:13:32 UTC, Andrei Alexandrescu wrote:
>> 1. Accept named `with`:
>>
>> with (auto xpath = extendPath(name)) {
>>     // the only name injected here is xpath
>> }
> 
> I've wanted this before, but not the "only name injected" thing, since 
> that defeats the whole point of `with`.
> 
> I'd want it to be a standard with statement, just with a name given for 
> the overall thing so you can refer to it as a whole in that scope too.
> 
>> with (auto xpath = extendPath(name).str) {
>>     // the only name injected here is xpath
>>     // assume str has type wstring, then xpath
>>     // also has type string
>> }
> 
> What's the difference between this and `if(auto xpath = ...)` ? It 
> wouldn't be null anyway.

Not generalizable.

> Or between it and
> 
> ---
> {
>    auto xpath = ....;
>    // stuff
> }
> ---
> 
> ?

In cases such as toStringz we don't care about the temporary (it's just 
a shell meant to ensure resource reclamation), we only care about using 
the slice within.

> That's why I think the with needs to keep its with behavior even if you 
> give it a name.

That would mess things up.


More information about the Digitalmars-d mailing list