A couple of extensions to `with` that would be worthwhile
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Wed Oct 13 11:13:32 UTC 2021
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.
2. Accept primitive types in named `with`:
with (auto xpath = extendPath(name).str) {
// the only name injected here is xpath
// assume str has type wstring, then xpath
// also has type string
}
Again, the destructor of the temporary object created is called AFTER
the with statement ends, even though the object is only used as a temporary.
3. Accept `with` with colon:
with (auto xpath = extendPath(name).str):
The meaning is like a scoped `with` that extends though the end of the
scope.
4. Accept `with` at top level.
All `with` forms that take a type should be accepted at top level. This
allows entire modules or fragments thereof to look names up in a
flexible manner.
More information about the Digitalmars-d
mailing list