5 ms·
This doesn't seem to allow single line if or while statements, because parentheses are optional. I like single line if when it is appropriate. (Python allows s
by sanxiyn 13y ago
This doesn't seem to allow single line if or while statements, because parentheses are optional.
I like single line if when it is appropriate. (Python allows single line if by using colons.) Apart from my taste, C preprocessor macros can't expand to more than a single line, so this seems to prevent macros expanding to contain control structures. Is there an easy fix?
- SupremumLimit 13y agoYou could do it by separating the body with a semicolon: if something_is_true(); log("warning!") I haven't really considered the preprocessor as I prefer to avoid it as much as possible.
- sanxiyn 13y agoThis doesn't work. "if a; if b; c; d" is then an ambiguous parse to both "if (a) { if (b) { c; d; } }" and "if (a) { if (b) { c; } d; }". If you decide to parse one way, how do you write the other one in a single line?
- SupremumLimit 13y agoI'd make this parse as "if (a) { if (b) { c; d; } }" and leave it at that. One of the reasons for significant indentation is to discourage this kind of code layout because it's hard to read.
- sanxiyn 13y agoOkay, concretely, I'd like to see a translation of this real-world C++ code (taken from V8): #define RETURN_OBJECT_UNLESS_EXCEPTION(ISOLATE, RETURN_VALUE, RETURN_EMPTY) \ if (!__allocation__.IsRetry()) { \ __object__ = __allocation__.ToObjectChecked(); \ if (__object__ == (ISOLATE)->heap()->exception()) { RETURN_EMPTY; } \ RETURN_VALUE; \ }
- SupremumLimit 13y agoThis seems to be missing backslashes. I haven't considered how to deal with preprocessor macros, but the if statement converts very simply: if !__allocation__.IsRetry() __object__ = __allocation__.ToObjectChecked() if __object__ == (ISOLATE)->heap()->exception(); RETURN_EMPTY RETURN_VALUE This isn't ambiguous like the example you suggested before.
- sanxiyn 13y agoThat doesn't work, because if statement is inside preprocessor macro and must be in a single line.
- SupremumLimit 13y agoI see what you mean. Yes, the if in a macro is a problem. I haven't addressed macros, that's a whole different can of worms.
- flogic 13y agoBut you can extend lines by putting a \ at the end. I think I was this while looking through some glibc code quite a long time ago. So, you can have a single "line" that happens to have multiple statements.