How to workaround assignment not allowed in a condition?
Steven Schveighoffer
schveiguy at gmail.com
Wed Oct 12 02:15:55 UTC 2022
Porting some C code to D
This results in an error:
```d
int x;
while(!(x = 5)) { break; }
```
Error is: assignment cannot be used as a condition, perhaps `==` was meant?
OK, fine, I'll use `==`:
```d
int x;
while(!(x = 5) == true) { break; }
```
Nope, same error. I tried reversing the operands. I also tried using it
in a boolean expression. As long as `=` appears in there, it gives me
the error.
The only thing I can get to work is to make an immediately called lambda:
```d
while(!(() => x = 5)()) { break; }
```
Is this seriously the only way to get around this? With other linting
errors, there's usually an easy workaround. like:
```d
if(x); // Error
if(x) {} // ok
```
Am I missing something?
-Steve
More information about the Digitalmars-d-learn
mailing list