Jump to page: 1 2
Thread overview
is there a reason declarative style if are allowed, but not while ?
Feb 26
deadalnix
Feb 27
Elronnd
Mar 01
deadalnix
Mar 02
user1234
Mar 02
user1234
Mar 02
RazvanN
February 26
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
On Friday, 26 February 2021 at 20:32:18 UTC, deadalnix wrote:
> while (auto foo = bar()) { ... }

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