Frank wrote:
Tom Verhoeff wrote:
Peter and I are discussing the possibility of including some kind of conditional operator, like `foo ? bar : baz' in C. Since GPC uses GCC's backend, this should be easy to do semantically, the main question is about syntax:
I would like to propose the following syntax:
<expr> if <cond> else <expr>
This is concise and nice (you can view "if <cond> else" as infix operator). Disadvantage: it requires priorities/parentheses for proper binding, it uses the if/else, which also appears in the conditional statement (this is also an advantage, since it does not require new reserved words/symbols).
This doesn't seem to cause parsing conflicts, AFAICS. However, it's quite different from `if' statements (and therefore maybe confusing), so I'd tend to favour the if-then-else version...
Because it is quite different, it is LESS confusing. If it would be very similar to the way the conditional statement is expressed, then it could be confusing, because when you quickly read some code you wouldn't immediately see whether you are dealing with a large conditional expression or just a conditional statement.
On the other hand, if you would only know that GPC supports conditional expressions, but you would not know the exact syntax, then most people would simply try the if-then-else version and not my proposed if-else. Therefore, the if-then-else seems preferable.
Tom
Tom Verhoeff wrote:
Frank wrote:
Tom Verhoeff wrote:
Peter and I are discussing the possibility of including some kind of conditional operator, like `foo ? bar : baz' in C. Since GPC uses GCC's backend, this should be easy to do semantically, the main question is about syntax:
I would like to propose the following syntax:
<expr> if <cond> else <expr>
This is concise and nice (you can view "if <cond> else" as infix operator). Disadvantage: it requires priorities/parentheses for proper binding, it uses the if/else, which also appears in the conditional statement (this is also an advantage, since it does not require new reserved words/symbols).
This doesn't seem to cause parsing conflicts, AFAICS. However, it's quite different from `if' statements (and therefore maybe confusing), so I'd tend to favour the if-then-else version...
Because it is quite different, it is LESS confusing. If it would be very similar to the way the conditional statement is expressed, then it could be confusing, because when you quickly read some code you wouldn't immediately see whether you are dealing with a large conditional expression or just a conditional statement.
This applies when used on the LHS (and then maybe even more so to the parser than tto humans -- as I've stated it seems to be rather difficult to parse `if cond then a else b := ...' as a conditional operator, while `a if cond else b := ...' seems to cause no parsing problems).
On the other hand, if you would only know that GPC supports conditional expressions, but you would not know the exact syntax, then most people would simply try the if-then-else version and not my proposed if-else. Therefore, the if-then-else seems preferable.
So it seems to come down to this:
Do we want it on the LHS? If so, I think we "must" use your proposal, and then, of course, also on the RHS.
If we don't need it on the LHS, then using `if ... then ...else' on the RHS seems preferable.
Opinions?
Frank
Frank Heckenbach a écrit :
So it seems to come down to this:
Do we want it on the LHS? If so, I think we "must" use your proposal, and then, of course, also on the RHS.
If we don't need it on the LHS, then using `if ... then ...else' on the RHS seems preferable.
For me the construct `a if cond else b := ...' looks too unnatural ...