1 matches bool, 2 matches long

Jonathan M Davis jmdavisProg at gmx.com
Fri Apr 26 12:37:39 PDT 2013


On Thursday, April 25, 2013 23:01:30 Walter Bright wrote:
> On 4/25/2013 10:49 PM, Ali Çehreli wrote:
> > It certainly behaves that way but it isn't an integer type and that's why
> > it is unintuitive.
> 
> But it is an integer type.

That was one of C's big mistakes. There's nothing whatsoever about bool that 
makes sense as an integral type. true and false have nothing to do with 1 and 
0 or any other integeral values. Having non-boolean values such as 1 and 0 
implicitly convert to bool under some set of circumstances can be very useful 
(which is why cast(bool) is implicitly used in conditions), but in the general 
case, it just causes bugs.

> The real issue is do you want to have the implicit conversions:
> 
> 0 => false
> 1 => true
> 
> or would you require a cast?

Emphatically yes.

The main place where casting would be annoying - if conditions and loop 
conditions - already insert an explicit cast underneat the hood. Other cases 
should require explicit casts; otherwise, we're just going to have bugs as has 
already been pointed out in this thread and has come up a number of times 
previously in the newsroup (one of the favorites being "foo" ~ true).

- Jonathan M Davis


More information about the Digitalmars-d mailing list