Quantcast
Channel: User CPlus - Programming Language Design and Implementation Stack Exchange
Viewing all articles
Browse latest Browse all 66

What are the drawbacks of allowing implicit boolean/integer conversions?

$
0
0

Some languages (C, C++, JavaScript, Python) allow one to use integers as booleans and vice versa:

int x;if (x) // Equivalent to: x != 0    y();

Or:

int x = 10 + (y > 10); // Equivalent to: y > 10 ? 11 : 10

However, other languages such as Java, C#, and Swift disallow this. They do not even allow explicit casting and force users to explicitly write != 0 and ? 1 : 0 to cast from integer to boolean or boolean to integer respectively.

What is the rationale for disallowing this? Allowing integers and booleans to freely convert allows certain logic to be expressed much more concisely, such as that to determine if exactly N conditions are true. So what are the drawbacks?


Viewing all articles
Browse latest Browse all 66

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>