Skip to content
Cameron Purdy edited this page Apr 7, 2020 · 3 revisions

A ternary expression is a well-known short-hand form of an “if statement”, but in the form of an expression:

a ? b : c

The type of the expression a must be Boolean. The implicit type of the expression is the bi-implicit type of the type of expression b and the type of expression c.

If the expression a yields True, then the expression yields the result of the expression b. If the expression a yields False or short-circuits, then the expression yields the result of the expression c.

The expression short-circuits if either expression b or expression c short-circuits.

Definite assignment rules:

  • The VAS before a is the VAS before the expression.
  • The VAS before b is the VAST after a.
  • The VAS before c is the VASF after a joined with the VAS from each short-circuiting point in a.
  • The VAS after the expression is the join of the VAS after b and the VAS after c.
  • If the type of the expression is Boolean, then:
    • The VAST after the expression is the join of the VAST after b and the VAST after c.
    • The VASF after the expression is the join of the VASF after b and the VASF after c.
    TernaryExpression:
        OrExpression
        OrExpression Whitespace ? OrExpression : TernaryExpression

Note the unusual requirement for whitespace before the ? operator, which differentiates this form of expression from the NotNullExpression. Among other inconveniences, it means that the following style of code popular in C will not compile:

return (flag?1:0)*value;

For purpose of compilation, place a space before the ?; for readability, it is suggested that spaces precede and follow both the ? and : symbols:

return (flag ? 1 : 0) * value;