Bools reloaded

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Fri Mar 3 08:05:25 PST 2006


xs0 wrote:
> 
> Hmm, how about an expansion of (d):
> 
> 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?
> 
> 2) forbid casts from bool to anything else (even explicit; besides 
> "foo?1:0" is shorter and more obvious than "cast(int)foo")
> 
> 3) when an expression involves a bool operand, all other operands get 
> converted to bool
> 
> 4) forbid all operators on bools, except ==, !=, &&, ||, &, |, and !. (& 
> and | don't short-circuit evaluation, but are otherwise equivalent to && 
> and ||).
> 
> 
> Then, the purists should be satisfied, because the only operations 
> allowed are the logic ones, and true and false don't have a numeric 
> representation.
> 

Ok, I (as a bool purist) will not be satisfied with that & and | part.
& and | are not logic operations but bit operations.

> There is a true/false representation of everything else, though, 
> allowing all the short forms we like, like
> 
> while(1)
> if (foo && foo.bar())
> if (refA || refB)
> 

Yuck, but I would agree with it as long as conditions have to be 
booleans. I know it isn't a perfect world and although I would never use 
the above, it would still be better than what we have now (as long as 
all the other conditions are satisified).



More information about the Digitalmars-d mailing list