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

Nick Treleaven nick at geany.org
Sat Feb 27 11:26:38 UTC 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.


More information about the Digitalmars-d mailing list