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

Antonio antonio at abrevia.net
Thu Oct 14 08:44:27 UTC 2021


On Wednesday, 13 October 2021 at 11:13:32 UTC, Andrei 
Alexandrescu wrote:
> We have an awkward idiom in the dmd source code:
>
> https://github.com/dlang/dmd/search?q=extendedPathThen
> https://github.com/dlang/dmd/search?q=toWStringzThen
>
> It's a tail wagging the proverbial dog. Clearly we could do 
> with better ways. We can improve the `with` statement as 
> follows:
>
> 1. Accept named `with`:
>
> with (auto xpath = extendPath(name)) {
>     // the only name injected here is xpath
> }
>
> All destructors for the expression involved in `with` are 
> called AFTER the with is done.
>

- I usually take the functional way:

```d
(string xpath){		
   ...
}( expandPath(name) );
```	

And UFCS doesn't help to "beutify" the code.  This doesn't work 
(And, in my opinion, should):

```d
expandPath(name).(auto xpath){		
   ...
};
```
This neither

```d
expandPath(name).((auto xpath){		
   ...
})();
```

May be "with" could be an alternative



More information about the Digitalmars-d mailing list