1 matches bool, 2 matches long
Rob T
alanb at ucora.com
Sat Apr 27 14:29:26 PDT 2013
On Saturday, 27 April 2013 at 20:31:15 UTC, Mehrdad wrote:
> The problem is 'bool' has *NOTHING* in common with integers!
>
> - Can't use + - * / << >> on bool's
Because currently D views booleans as a 1 bit int, you can do
seemingly nonsensical things with the bool type
bool b = false + true; // OK
bool b = true * true; // OK
bool b = true / true; // OK
bool b = true + true; // compile error because 2 does not fit
into 1 bit.
Even if you accept the view that bool is a 1 bit int, this
conditional makes no sense at all
int x = -123456;
if (x) then
{
// evaluates to true, but why???
}
if (x == true) then
{
// false because x != true(1), even though previously it was
true
}
If bools are 1 bit ints, then why do we have 'true' and 'false'
as keywords? Those keywords only serve to confuse the programmer
into thinking that the bool type is actually a real boolean type.
The worse thing about this situation is the inconsistencies. It
would not be nearly as bad if bool really was a 1 bit int type,
instead it sometimes behaves like an int and sometimes it behaves
like a boolean. Why name name the type "bool" instead of "bit"?
Why have true and false keywords?
What I gather from the state of affairs, is an attempt to create
the illusion of a boolean while retaining all of the properties
of a 1 bit int type, with the notable exception of the weird
conditional that evaluates an integer to true if it is not 0.
--rt
More information about the Digitalmars-d
mailing list