Bools reloaded

Oskar Linde oskar.lindeREM at OVEgmail.com
Fri Mar 3 11:06:40 PST 2006


xs0 wrote:
> Don Clugston wrote:
>> And if you read that post, you will find that Derek said:
>>
>> ** It can be explicitly cast to an integer such that false is 0, and 
>> true is 1.
>>
>> whereas Kyle said:
>>
>> integer operations are illegal, and it cant be cast to anything.
>>
>> So there are at least four types of bool being discussed. Probably more.
>> And at least two can be called "pure bool".
>> (a) the existing bool
>> (b) Bruno and I are in here somewhere, and are not necessarily the same.
>> (c) Derek
>> (d) Kyle
>>
>> Right now, (a) and (c) are the only ones which are clearly defined.
> 
> Hmm, how about an expansion of (d):

Yes, this is very similar to the definition I have suggested. I think 
this is a good compromise:

> 1) still allow implicit casts to bool from any type, as in
> - value != 0
> - obj_ref !is null
> - pointer !is null
> - array.ptr !is null
> - !struct.opEquals(0) // does this work now?

This would mean that we would keep if(a && b) and also that the type of 
any conditional could be defined as bool. But it would also mean that 
you could write bool b = 5; I guess.

> 2) forbid casts from bool to anything else (even explicit; besides 
> "foo?1:0" is shorter and more obvious than "cast(int)foo")

I can live with explicit cast both being allowed and not. I don't see it 
as an important detail. The important part is to disallow implicit casts 
from bool to any type.

> 3) when an expression involves a bool operand, all other operands get 
> converted to bool

This is the logical conclusion from 1) and 2).

> 4) forbid all operators on bools, except ==, !=, &&, ||, &, |, and !. (& 
> and | don't short-circuit evaluation, but are otherwise equivalent to && 
> and ||).

For consistency, ~ should be supported too. And ^ too of course.

/Oskar



More information about the Digitalmars-d mailing list