Closed heinthanth closed 2 years ago
It’s a common idiom to prevent errors using the macro in a block statement, such as if
, without curly braces
if (cond)
BINARY_OP(…);
will still work as expected, with all the usual rules of block scope and semi-colon terminations. This wouldn’t be the case if the BINARY_OP
didn’t have the do … while(false);
Another reason is that it forces a semicolon after the macro. Without the do...while(false)
, i.e. with just braces ({...}
), no semicolon is required after the macro.
The strange form of that macro is explained in the book here. C is weird.
Is there any specific reason to use that do while loop since
while(false)
make the loop runs only once. I see that in both Lox and Wren VM.in https://github.com/wren-lang/wren/blob/accfa598b3cbc54fdfb33cb568055ed6994a4966/src/vm/wren_common.h#L160-L168
in https://github.com/wren-lang/wren/blob/accfa598b3cbc54fdfb33cb568055ed6994a4966/src/vm/wren_common.h#L160
That code works. But I just want to know using do-while instead of just write the code inside without loop.