Casting an expression to bool means testing for 0 or !=0 for arithmetic types
Jonathan M Davis
jmdavisprog at gmail.com
Sat Jul 31 16:40:20 PDT 2010
On Saturday 31 July 2010 16:22:46 Pluto wrote:
> But these aren't compatible for the same function.
> Defining false as <1 would fix this.
Historically, false has always been 0 and true non-zero - probably because it's
then easy to use the assembly instruction to check whether something is 0 (which
I believe is cheaper than checking for a specific value).
In any case, changing that would likely break both people's expectations and
their code. And regardless of that, it _isn't_ an error code to have a function
return whether it succeeded or not. If you want to check error codes, then check
for specific values or actually use the returned value rather than just
throughing it in a condition by itself.
_Every_ type converts to bool when put in a condition statement. It's entirely
consistent that way. And having 0 be false and 1 true for numeric types is
entirely consistent with how things have historically worked and pretty much
everyone will expect them to work. Changing that would cause far more problems
than it would ever solve. If you want error codes, you can just check the return
value against 0 like has historically been done and you're fine, but error codes
are generally a poor choice anyway.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list