David Nicol wrote:
>Challenge: Can anyone come up with an ambiguous expression where it is
>not clear if the expression is supposed to be an R-value if-statement
>(were such things allowed) or a trailing conditional?
Not a global ambiguity, because the "){" sequence that you must have in
the if-expression can't appear at the top level of an expression to be
part of a trailing-if modifier. You can have ambiguity to the extent that
you can't tell what kind of if it is until that close paren is reached,
which poses a bigger problem than it sounds like, because the type of if
determines the scoping of the intervening parenthesised expression, and
the scoping has to be put into effect before the expression is parsed.
You can get this much ambiguity from a sub arg list that's allowed to
be null: "foo if (1) ..." could be "foo() if (1) ;" or "foo(do { if(1)
{...} })".
There *is* a global ambiguity between prefix-if-expression and
prefix-if-statement. "if (1) { ... } + 1;" can be one statement
(expression statement with addition at top level) or two (if statement
followed by constant expression statement).
This has been explored before. Treating an if statement as an expression
can't be acceptably done without some syntactic marker.
-zefram
Thread Previous