is there a reason declarative style if are allowed, but not while ?

Timon Gehr timon.gehr at gmx.ch
Sun Feb 28 14:57:44 UTC 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);
}


More information about the Digitalmars-d mailing list