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

deadalnix deadalnix at gmail.com
Tue Sep 25 10:59:57 PDT 2012


Today, I spent quite a lot of time because a stupid problem. It 
basically boil down to :

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

Obviously, I intended to write
string s = "somestring" ~ (b?"":"somemorestring");

but, due to operator priority, it is means
string s = ("somestring" ~ b)?"":"somemorestring";

So what ? So it compile without even a warning. I missed the error 
because I expected to get a type error in such case. So I assumed that 
the code was correct.

After checking in TDPL, it is stated that ubyte and char should 
implicitly cast to one another. And as bool implicitly convert to ubyte, 
this is effectively the specified behavior.

I think this is a rather problematic behavior. What is the point of have 
both byte and char types if both are implicitly convertible to one 
another (and have completely different semantic) ?


More information about the Digitalmars-d mailing list