digitalmars.D - The `with` construct is good, but it could be great
- Quirin Schroll (16/16) Jul 20 2022 The [`with`
- Stefan Koch (4/8) Jul 20 2022 I have implemented `with(Sym):`
- jmh530 (8/22) Jul 20 2022 `with Symbol:` or `with(Symbol):` seems natural. Would `with
- Nick Treleaven (5/10) Jul 30 2022 Unless it was special cased, that would fall back to top level
- FeepingCreature (10/15) Aug 08 2022 What I supported in an old language is:
- IGotD- (10/13) Jul 20 2022 One of the big usage of "with" in C# is that the destructor (on
- jmh530 (3/9) Jul 20 2022 D already has `with`...
- IGotD- (3/5) Jul 20 2022 Wow, I totally missed that. A bit different from C# though and
- max haughton (4/18) Jul 20 2022 The main reason why with expressions are desired is so they can
- Petar Kirov [ZombineDev] (7/31) Jul 29 2022 Agreed, `with` (along with `let` bindings) is pretty common in
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.
Jul 20 2022
On Wednesday, 20 July 2022 at 16:46:10 UTC, Quirin Schroll wrote:[...] Up for discussion, I’d even suggest a no-brace form for `with` *declarations:* * `with Symbol:` or `with(Symbol):` (like attributes)I have implemented `with(Sym):` The patch is very short and should apply cleanly https://github.com/UplinkCoder/dmd/commit/492ea4c0e17127e82982672553e33b1b6eb0491f
Jul 20 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.
Jul 20 2022
On Wednesday, 20 July 2022 at 16:53:27 UTC, jmh530 wrote: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.Unless it was special cased, that would fall back to top level imports if the modules named didn't exist in std.math. (Following usual rules for with). So when seeing `with... import` you'd have to be aware of that. Maybe other syntax would be clearer.
Jul 30 2022
On Wednesday, 20 July 2022 at 16:53:27 UTC, jmh530 wrote: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.What I supported in an old language is: ``` import std.math.(constants, hardware); ``` Which has the advantage of not falling back to a toplevel import if not found. But I don't think I'll do that in my current language, because it's sort of a library smell if you need a page full of imports. Just `import std;`.
Aug 08 2022
On Wednesday, 20 July 2022 at 16:46:10 UTC, Quirin Schroll wrote: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 class that is in the with expression) is guaranteed to be run when the destructor is executed but the "with" statement makes it deterministic. Does D need a "with" statement? I don't know and it also depends how the libraries are written. I'm personally not completely against introducing "with" in D. D has tried to follow Rust
Jul 20 2022
On Wednesday, 20 July 2022 at 18:15:10 UTC, IGotD- wrote:[snip] Does D need a "with" statement? I don't know and it also depends how the libraries are written. I'm personally not completely against introducing "with" in D. D has tried to a better fit.D already has `with`... https://dlang.org/spec/statement.html#with-statement
Jul 20 2022
On Wednesday, 20 July 2022 at 19:21:38 UTC, jmh530 wrote:D already has `with`... https://dlang.org/spec/statement.html#with-statementcould certainly be expanded upon.
Jul 20 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.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.
Jul 20 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: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_statementThe [`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.
Jul 29 2022