Bools reloaded

xs0 xs0 at xs0.com
Fri Mar 3 14:40:31 PST 2006


Oskar Linde wrote:
> xs0 wrote:
> 
>> Don Clugston wrote:
>>
>>> 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".
 >>> ...
>>> (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.

Yup, but I think it's good that with

bool b=expr;
if (b) {
}

the if statement's condition always evaluates exactly the same as with

if (expr) {
}

even though the "=5" case indeed looks somewhat odd. It should be 
declared bad style to write that :)


>> 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.

Well, if you allow/provide a cast, it's most probably going to be to 0 
and 1, which somewhat implies sort-of equivalence between 1 and true, 
and that's probably not a good thing.. it would mean that it'd still be 
possible to do arithmetics with booleans, which I believe should be avoided.


>> 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.

Well, we have ! and != already, I'm not sure what there's to gain from 
having duplicates?


xs0



More information about the Digitalmars-d mailing list