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

Petar Petar
Fri Jul 29 08:25:27 UTC 2022


On Thursday, 21 July 2022 at 01:41:03 UTC, max haughton wrote:
> 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.
>
> The main reason why with expressions are desired is so they can 
> be used in expressions, adding a form that does not introduce a 
> scope does not change this.

Agreed, `with` (along with `let` bindings) is pretty common in 
functional languages which are commonly expression based. Here's 
an example from a language that I use on a daily basis:

* 
https://nixos.org/manual/nix/stable/expressions/language-constructs.html#with-expressions
* https://nixos.wiki/wiki/Nix_Expression_Language#with_statement


More information about the Digitalmars-d mailing list