implicit conversion from bool to char, is it really necessary ?

bearophile bearophileHUGS at lycos.com
Tue Sep 25 12:24:53 PDT 2012


deadalnix:

> bool b; // Defined elsewhere.
> string s = "somestring" ~ b?"":"somemorestring";

bool => char implicit conversion is not commonly useful in 
programs, but I think it's also not a common bug.

On the other hand in past we have discussed a little about the 
bug-prone precedence of the ?: operator. Analysis of shared code 
repositories shows that this is a common source of bugs. So I 
think avoiding this bug has higher priority (and it's enough to 
avoid your specific bug).

One of the ideas for D was that when the ?: is included in a 
larger expression, to requires 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; // error
auto x5 = y1 ? z1 : (y2 ? z2 : w2); // OK
auto x6 = y1 ? z1 : y2 ? z2 : w2; // 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.

Bye,
bearophile


More information about the Digitalmars-d mailing list