www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - is there a reason declarative style if are allowed, but not while ?

reply deadalnix <deadalnix gmail.com> writes:
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
next sibling parent reply Elronnd <elronnd elronnd.net> writes:
On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:
 while (auto foo = bar()) { ... }
for (T foo; foo = bar();) { ... }
Feb 26 2021
parent Nick Treleaven <nick geany.org> writes:
On Saturday, 27 February 2021 at 00:05:29 UTC, Elronnd wrote:
 On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:
 while (auto foo = bar()) { ... }
for (T foo; foo = bar();) { ... }
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.
Feb 27 2021
prev sibling next sibling parent reply Tobias Pankrath <tobias+dlang pankrath.net> writes:
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
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 28.02.21 11:25, Tobias Pankrath wrote:
 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
+1, this leads to kludges such as: for(;;){ bool cond = <expr>; if(!cond) break; } { bool cond; do{ cond=<expr>; }while(cond); }
Feb 28 2021
prev sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
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
next sibling parent Max Haughton <maxhaton gmail.com> writes:
On Sunday, 28 February 2021 at 14:32:32 UTC, Timon Gehr wrote:
 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++.
The with() issue linked within that bug would be nice - in particular I really want trailing with like `{} with (...)` like a where clause in Haskell
Feb 28 2021
prev sibling parent reply deadalnix <deadalnix gmail.com> writes:
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
next sibling parent reply user1234 <user1234 12.de> writes:
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:
 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.
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.
Mar 02 2021
parent user1234 <user1234 12.de> writes:
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
prev sibling parent RazvanN <razvan.nitu1305 gmail.com> writes:
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:
 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.
PR: https://github.com/dlang/dmd/pull/12246
Mar 02 2021