Booleans (was Re: My .02 on PEP308 + an extra question)

Stephen Horne intentionally at blank.co.uk
Sun Mar 2 08:57:22 EST 2003


On Mon, 10 Feb 2003 17:44:21 -0800, Erik Max Francis <max at alcyone.com>
wrote:

>Aahz wrote:
>
>> Same reason that Guido finally broke down and added booleans:
>> standardized spellings are worth a lot.
>
>This is true, but there's more to Booleans than just standardized
>spellings.  We all could live with False, True = 0, 1, but their
>introduction makes them a full-fledged type.

I'm not convinced by this. In C++, 'bool', 'true' and 'false' are
pretty pointless. The Ada equivalents, however, are not. The reason
being, in C++ 'true' and 'false' cast implicitly to/from integers.

Given this behaviour, the keywords 'true' and 'false' are no more
useful than library definitions such as '#define TRUE 1'. The weak
typing due to the implicit conversion destroys most, if not all,
benefits of a separate type.

In Ada, this is not true. If you want integer equivalents, you need to
ask for them. The boolean type is therefore a strong declaration of
intent that the compiler uses for error checking - use an integer
where a boolean is expected (or visa versa) and you get an error
message.

Historically, the C++ situation happened because (1) C++ inherited
from C at a time when C did not have a boolean type, and (2) C++ basic
types are pretty weak (from the C heritage again) anyway. Ada had
booleans from the start (so it did not need an implicit integer
equivalence) and is strongly typed.

Add on the additional problem of C programmers who think their
programs are C++ just because they have a '.cpp' extension and a few
'//' comments, and the 'bool' type is really not that useful.

The most common effect of using 'bool' in Visual C++, for instance, is
that you constantly get warned that casts from integer types to bool
are inefficient (because the compiler forces the value to '1' rather
than just non-zero, though strictly it doesn't need to - it should
only do this on casting bool to int when the source bool is from an
uncertain source, really).

I suspect the Python history is too close to the C++ history for
comfort - the boolean type cannot be a strong type because of
backwards compatibility issues. I'm not clear on what the benefit now
from adding a separate type is - except for the standard spelling and
the declaration of intent (useful to human readers only) inherent in
using that spelling.

-- 
steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list