Bools reloaded
xs0
xs0 at xs0.com
Fri Mar 3 09:17:10 PST 2006
Ivan Senji wrote:
> xs0 wrote:
>>
>> 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.
They are bitwise operations on integers, because that's what they're
defined to be. We're free to define them to do something else on
booleans, which I propose to be [what I said above].
BTW, even Java, which has a really purist boolean implementation, uses
those for exactly the stated purpose (and even calls them "logical
operators", while && and || are "conditional-and" and "-or" :)
http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.5
>> 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).
I used to think so too, until I was forced to code in Java :) Even
though I don't mind typing something as simple as
if (a!=null)
the verbosity of longer expressions really annoys the heck out of me.
Now, I prefer
> if (a && b && c && d) // check for nulls
> ...
to
> if (a!=null && b!=null && c!=null && d!=null)
> ...
or D's even verboser
> if (a !is null && b !is null && c !is null || d !is null)
> ...
Note how the vars that are being tested get lost in there.. BTW, did you
notice the last line is not equivalent? :)
xs0
More information about the Digitalmars-d
mailing list