Can we drop casts from float to bool?

grauzone none at example.net
Tue Mar 23 13:16:08 PDT 2010


Don wrote:
>> In D some people have proposed to change the semantics of the "is" 
>> operator, to make it more useful and tidy, so if you want to know if x 
>> is a NaN you can then write if(x is nan).
> That won't work in this case, unless you make nan a keyword. There are 
> 2^^62 different NaNs, so a bitwise comparison will not work.

There are other cases where if and is seem to do non-obvious things, 
that can lead to subtle bugs. If "x" is an array, if(x) checks if the 
array descriptor is null, and not whether x has length 0.

To be consistent, I see two choices how this could be fixed:
1. "if" is "intelligent", then both the float and the array case should 
be fixed to do what one would expect
2. just turn if(x) into if(x is x.init) and make "is" a bitwise 
comparison even for floats

If way 2. is chosen, I don't think multiple NaN would be a problem. The 
programmer just has to be aware that NaN isn't a single value. "is" 
yields true if the value is exactly the same, and nothing else. In 
conclusion, if(x) would be about as useless for floats as it is for 
arrays. In any way, the situation wouldn't be worse than before, but at 
least more consistent.



More information about the Digitalmars-d mailing list