Summary: Require parenthesization of ternary operator when
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2012-10-04 09:47:28 PDT ---
In past we have discussed in the D newsgroups about the bug-prone precedence of
the ?: operator. Analysis of shared code repositories (and articles about
static code analysis) shows that this is a common source of bugs. So I suggest
to look for wasy to avoid/reduce such bugs in D code.

One of the possible ideas is (this is a small breaking change): when the ?: is
included in a larger expression, require parentheses around it.

auto x1 = y1 ? z1 : w1; // OK
auto x2 = x0 + (y1 ? z1 : w1); // OK
auto x3 = (x0 + y1) ? z1 : w1; // OK
auto x4 = x0 + y1 ? z1 : w1; // Compilation error
auto x5 = y1 ? z1 : (y2 ? z2 : w2); // OK
auto x6 = y1 ? z1 : y2 ? z2 : w2; // Compilation error

In theory this increases the number of parentheses a little, but in practice in
many similar situations I already put those parentheses, for readability and to
avoid some of my mistakes.

Ideas for other solutions are welcome.

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to