digitalmars.D - is there a reason declarative style if are allowed, but not while ?
- deadalnix (6/6) Feb 26 2021 D allows for this type of constructs:
- Elronnd (2/3) Feb 26 2021 for (T foo; foo = bar();) { ... }
- Nick Treleaven (15/18) Feb 27 2021 I get an error, even if I put brackets around the condition:
- Tobias Pankrath (6/13) Feb 28 2021 What's also unfortunate is
- Timon Gehr (12/32) Feb 28 2021 +1, this leads to kludges such as:
- Timon Gehr (4/14) Feb 28 2021 I don't think there's any good reason.
- Max Haughton (4/19) Feb 28 2021 The with() issue linked within that bug would be nice - in
- deadalnix (3/6) Mar 01 2021 Is there a way to expedite this? This seems like an obvious
D allows for this type of constructs: if (auto foo = bar()) { ... } I was trying to do something similar with a while loop, but DMD seems very upset about it. Is there a reason to disallow the following? while (auto foo = bar()) { ... }
Feb 26 2021
On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:while (auto foo = bar()) { ... }for (T foo; foo = bar();) { ... }
Feb 26 2021
On Saturday, 27 February 2021 at 00:05:29 UTC, Elronnd wrote:On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:I get an error, even if I put brackets around the condition: forauto.d(33): Error: assignment cannot be used as a condition, perhaps `==` was meant? Supposing we allow it though. Besides being awkward for type inference (in general), your version doesn't work with const: ``` const(S) bar(); int main(string[] args) { for (const S foo; (foo = bar());) { } } ``` forauto.d(33): Error: cannot modify `const` expression `foo` The while/auto version could work with const.while (auto foo = bar()) { ... }for (T foo; foo = bar();) { ... }
Feb 27 2021
On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:D allows for this type of constructs: if (auto foo = bar()) { ... } I was trying to do something similar with a while loop, but DMD seems very upset about it. Is there a reason to disallow the following? while (auto foo = bar()) { ... }What's also unfortunate is do { bool cond = <expr>; } while(cond) does not work.onlineapp.d(5): Error: undefined identifier cond
Feb 28 2021
On 28.02.21 11:25, Tobias Pankrath wrote:On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:+1, this leads to kludges such as: for(;;){ bool cond = <expr>; if(!cond) break; } { bool cond; do{ cond=<expr>; }while(cond); }D allows for this type of constructs: if (auto foo = bar()) { ... } I was trying to do something similar with a while loop, but DMD seems very upset about it. Is there a reason to disallow the following? while (auto foo = bar()) { ... }What's also unfortunate is do { bool cond = <expr>; } while(cond) does not work.onlineapp.d(5): Error: undefined identifier cond
Feb 28 2021
On 26.02.21 21:32, deadalnix wrote:D allows for this type of constructs: if (auto foo = bar()) { ... } I was trying to do something similar with a while loop, but DMD seems very upset about it. Is there a reason to disallow the following? while (auto foo = bar()) { ... }I don't think there's any good reason. Related: https://issues.dlang.org/show_bug.cgi?id=16140 FWIW, note that this works in C++.
Feb 28 2021
On Sunday, 28 February 2021 at 14:32:32 UTC, Timon Gehr wrote:On 26.02.21 21:32, deadalnix wrote:The with() issue linked within that bug would be nice - in particular I really want trailing with like `{} with (...)` like a where clause in HaskellD allows for this type of constructs: if (auto foo = bar()) { ... } I was trying to do something similar with a while loop, but DMD seems very upset about it. Is there a reason to disallow the following? while (auto foo = bar()) { ... }I don't think there's any good reason. Related: https://issues.dlang.org/show_bug.cgi?id=16140 FWIW, note that this works in C++.
Feb 28 2021
On Sunday, 28 February 2021 at 14:32:32 UTC, Timon Gehr wrote:I don't think there's any good reason. Related: https://issues.dlang.org/show_bug.cgi?id=16140 FWIW, note that this works in C++.Is there a way to expedite this? This seems like an obvious improvement.
Mar 01 2021
On Monday, 1 March 2021 at 15:20:10 UTC, deadalnix wrote:On Sunday, 28 February 2021 at 14:32:32 UTC, Timon Gehr wrote:This may requires a DIP. Occasionally small languages changes are accepted without so you might try a PR, then if you're said that a DIP is required you can mention in the document that there's a draft PR. Finally this also could be a a candicate for a "-preview" feature. It's also interesting to note that for the IfStatement, a declaration is allowed because from the AST point of view you just need a supplmental storage class. So you have a "StorageClass Expression" not "Expression | VariableDeclaration". For the WhileStatement that should be implemented in the same fashion.I don't think there's any good reason. Related: https://issues.dlang.org/show_bug.cgi?id=16140 FWIW, note that this works in C++.Is there a way to expedite this? This seems like an obvious improvement.
Mar 02 2021
On Tuesday, 2 March 2021 at 16:24:18 UTC, user1234 wrote:[...] It's also interesting to note that for the IfStatement, a declaration is allowed because from the AST point of view you just need a supplmental storage class. So you have a "StorageClass Expression" not "Expression | VariableDeclaration"Just ignore this paragraph. Actually there's also a Type allowed so it's rather. StorageClass? Type? Expression
Mar 02 2021
On Monday, 1 March 2021 at 15:20:10 UTC, deadalnix wrote:On Sunday, 28 February 2021 at 14:32:32 UTC, Timon Gehr wrote:PR: https://github.com/dlang/dmd/pull/12246I don't think there's any good reason. Related: https://issues.dlang.org/show_bug.cgi?id=16140 FWIW, note that this works in C++.Is there a way to expedite this? This seems like an obvious improvement.
Mar 02 2021