Jump to page: 1 2
Thread overview
is there a reason declarative style if are allowed, but not while ?
Feb 26, 2021
deadalnix
Feb 27, 2021
Elronnd
Feb 27, 2021
Nick Treleaven
Feb 28, 2021
Tobias Pankrath
Feb 28, 2021
Timon Gehr
Feb 28, 2021
Timon Gehr
Feb 28, 2021
Max Haughton
Mar 01, 2021
deadalnix
Mar 02, 2021
user1234
Mar 02, 2021
user1234
Mar 02, 2021
RazvanN
February 26, 2021
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()) { ... }


February 27, 2021
On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:
> while (auto foo = bar()) { ... }

for (T foo; foo = bar();) { ... }
February 27, 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:
>> 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.
February 28, 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

February 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++.
February 28, 2021
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
February 28, 2021
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);
}
March 01, 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.
March 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:
>> 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.
March 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:
>> 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
« First   ‹ Prev
1 2