Bools reloaded
Oskar Linde
olREM at OVEnada.kth.se
Fri Mar 3 12:18:19 PST 2006
Ivan Senji wrote:
> Oskar Linde wrote:
>> Ivan Senji wrote:
>>
>>> Don Clugston wrote:
>>>
>>>> Sorry, that's still not clear.
>>>> Bruno is right, terms like "pure bools" or "purist bools" are vague,
>>>> you can't expect everyone to know what you mean.
>>>>
>>>> For example: do you want "&" to be legal for bool types, or just "&&"?
>>>
>>>
>>> Just &&.
>>
>>
>> Why? Ever heard of boolean algebra?
>
> Ever heard of logic operators && and ||? That is what they are for.
&& and || controls the program flow. They are not algebraic operators.
&,|,~,^ are real operators that also maps directly onto machine
instructions on most cpu architectures. Their meaning would be well defined
for bools. && and || requires branching and are therefore much less
efficient (even though this can be optimized away in many cases).
Also, not allowing ^ for bools would mean that code that reads a^b today
would have to be rewritten as:
(a && !b) || (!a && b).
>> Why should &,|,^,~ not be defined
>> and allowed for bool? It would be problematic and inconsistent not
>> having non-short-circuit operators for booleans.
>
> I learned from xs0's reply that Java uses &,| for non-short-circuit
> evaluation when arguments are of type bool. As D doesn't have a bool in
> the Java meaning I don't think making & anything but bitwise operator
> makes sense.
Huh? There have been regular discussions on the D newsgroups about bool
since atleast 2001. Almost all threads about this have people arguing for
the introduction of a logical (as opposed to the current numerical) bool
type similar (not necessarily identical) to the Java boolean. Isn't this
what we are discussing here too?
>>>> (they mean very different things for integers). "&" normally means
>>>> "bitwise and", but a pure bool doesn't have any bits.
>>>> Is it legal to cast from a bool to some other type?
>>>
>>>
>>> I'm not against casting but implicit conversions are bad.
>>
>>
>> Do you mean that all implicit conversions are bad? Are the integer
>> promotion rules bad?
>
> Ofcourse not. But with bools yes.
I read your statement as meaning that all implicit conversions are bad. I
was merely interested in why you thought so. :)
/Oskar
More information about the Digitalmars-d
mailing list