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

Adam D Ruppe destructionator at gmail.com
Wed Oct 13 13:05:06 UTC 2021


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.

Or between it and

---
{
   auto xpath = ....;
   // stuff
}
---

?


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


More information about the Digitalmars-d mailing list