In c, you can have code like this:

static void wtest( void ) {
  int f;
  while ( ( f = some_val( ) ) ) {
    printf(" our value is now: %d\n", f );
  }
}

gcc compiles this without warning or error (at least if you use the double parentheses to assure the compiler that you realize you are testing an assignment, not a comparison).

I would like to do the same thing in d, something like this:

private void wtest( ) {
  int f;
  while ( ( f = some_val( ) ) ) {
    writeln(" our value is now: ", f );
  }
}

or even better:

private void wtest( ) {
  while ( ( auto f = some_val( ) ) ) {
    writeln(" our value is now: ", f );
  }
}

This however does not work, and the gdc compiler says "assignment cannot be used as a condition, perhaps == was meant?"

I don't absolutely have to do it this way, as i guess i could do 'while (true) {...' and then break if the assignment returns zero.

But i really, really would like to use the idiom of assigning a value from inside a while condition.

Is it possible to do this? Perhaps with extra braces or something, like while ( {something here} ) { .... } ?

TIA for any pointers or advice.

dan

Reply via email to