The `with` construct is good, but it could be great

jmh530 john.michael.hall at gmail.com
Wed Jul 20 16:53:27 UTC 2022


On Wednesday, 20 July 2022 at 16:46:10 UTC, Quirin Schroll wrote:
> The [`with` 
> statement](https://dlang.org/spec/statement.html#with-statement) is a good feature. However, a lot of people including me believe that it lacks potential to be great.
>
> It introduces scope and cannot be used in a declarative scope.
>
> It’s shortcomings are surprisingly similar to C++’s `if 
> constexpr` that Andrei in [*Design by 
> Introspection*](https://www.youtube.com/watch?v=k31wZafAMhk&t=1712) considered to be a “fatal mistake”.
>
> Up for discussion, I’d even suggest a no-brace form for `with` 
> *declarations:*
> * `with Symbol:` or `with(Symbol):` (like attributes)
> * `with Symbol;` (like `import`)
>
> That implemented, `with` would be a great feature.
>
> Contrary to others, I do not believe that `with` expressions 
> would be necessary. The main reason `with` expressions are 
> asked for is because `with` introduces scope.

`with Symbol:` or `with(Symbol):` seems natural. Would `with 
Symbol;` do the same thing? Seems harder to follow than the above.

On a recent bug report I suggested `with` work with `import` so 
that you could do something like `with(std.math) import 
constants, hardware;` that would be equivalent to `import 
std.math.constants, std.math.hardware;`. That seems like a 
natural extension.


More information about the Digitalmars-d mailing list