Bools reloaded

Ivan Senji ivan.senji_REMOVE_ at _THIS__gmail.com
Fri Mar 3 13:02:50 PST 2006


xs0 wrote:
> 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].

Ok, now I understand what you're saying. But to have that behaviour we 
need very very different bools.

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

Thanks for the link.

> 
>>> 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 :) 

I also coded in Java and never found this to be a problem :)

> 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

But what is going on behind the scene if a,b,c,d are ints or ponters,
conversions to bools? I still don't get it how logic && operator can be 
aplied to integers. What is 5 && 3? (Except two numbers: 5,3.) It has no 
value regarding truth. It is neither true nor false.


>  >    ...
> 
> to
> 
>  > if (a!=null && b!=null && c!=null && d!=null)

better.... :)

>  >    ...
> 
> or D's even verboser
> 
>  > if (a !is null && b !is null && c !is null || d !is null)

the best :)

Except that little bug caused deliberately by you :)

Formated this way:

if( (a !is null) && (b !is null) && (c !is null) && (d !is null) )

it is alittle bit more clear.



More information about the Digitalmars-d-announce mailing list