www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using assignment statement as conditional in a where

reply dan <dan.hitt gmail.com> writes:
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
Dec 03 2016
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 03/12/2016 9:55 PM, dan wrote:
 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
If you can use another compiler do so, gdc is on an old frontend/Phobos now. I recommend ldc or you know the reference compiler dmd if performance/platform isn't an issue (not that dmd can't produce decent codegen). This does compile: int func() { return 0; } void main() { int x; while((x = func()) != 0) { } }
Dec 03 2016
parent reply dan <dan.hitt gmail.com> writes:
On Saturday, 3 December 2016 at 09:03:25 UTC, rikki cattermole 
wrote:
 On 03/12/2016 9:55 PM, dan wrote:
 [...]
If you can use another compiler do so, gdc is on an old frontend/Phobos now. I recommend ldc or you know the reference compiler dmd if performance/platform isn't an issue (not that dmd can't produce decent codegen). This does compile: int func() { return 0; } void main() { int x; while((x = func()) != 0) { } }
Thanks Rikki, that works great and is nearly ideal (doesn't seem to allow 'auto' but probably that's some scoping issue). I do prefer gdc because it is gpl'ed, but appreciate any suggestions. Thanks again for your help! dan
Dec 03 2016
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 04/12/2016 7:26 AM, dan wrote:
 On Saturday, 3 December 2016 at 09:03:25 UTC, rikki cattermole wrote:
 On 03/12/2016 9:55 PM, dan wrote:
 [...]
If you can use another compiler do so, gdc is on an old frontend/Phobos now. I recommend ldc or you know the reference compiler dmd if performance/platform isn't an issue (not that dmd can't produce decent codegen). This does compile: int func() { return 0; } void main() { int x; while((x = func()) != 0) { } }
Thanks Rikki, that works great and is nearly ideal (doesn't seem to allow 'auto' but probably that's some scoping issue). I do prefer gdc because it is gpl'ed, but appreciate any suggestions. Thanks again for your help! dan
The only thing in gdc that is GPL is the backend and glue layer. The frontend is the same one in ldc and dmd.
Dec 03 2016