implicit conversion from bool to char, is it really necessary ?
deadalnix
deadalnix at gmail.com
Tue Sep 25 13:27:17 PDT 2012
Le 25/09/2012 21:24, bearophile a écrit :
> 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.
>
Thi sis reversed logic. bool => char have know drawback. So it must
bring something at least as important as the drawback (whatever how
small it is) involved.
I also wanted to discuss this subject specifically because it was a
surprise to me and to other people on IRC. So I guess this feature is
counter intuitive and contradict the rule that state that thing should
work the way you expect them to work.
I also already knew that ?: were sometime confusing. However, I skipped
on that because I was sure (and wrong) that a type error would be triggered.
It is a surprising behavior, so something you want to avoid as much as
possible when creating a programing language.
> 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
This would solve my particular bug, and I think it is a good thing to
make this clearer.
More information about the Digitalmars-d
mailing list