bool <=> int. WTF. (DIP0015)

wjoe invalid at example.com
Thu Jul 4 15:30:59 UTC 2019


On Tuesday, 2 July 2019 at 16:24:07 UTC, Exil wrote:
> https://www.youtube.com/watch?v=cpTAtiboIDs#t=2h17m50s
>
> This isn't likely to change.
>
> Some more discussion here:
>
> https://forum.dlang.org/thread/bareupbeyubtmyorxqcz@forum.dlang.org?page=1

Technically this is a reply to the video on youtube.

A Boolean should be a type to be explicitly incompatible with an 
int with values of 'true' and 'false' otherwise there is no 
justification for its existence.
Being implemented as an int with a range of 0..1 is an 
implementation detail. It could just as well be implemented as 
all bits 1 = false, all bits 0 = true, or -17 = false and 42 = 
true, or ... .

I strongly disagree with the *definition* of int 0..1 because a 
Boolean isn't a number and is neither 0 nor 1, but it's true or 
false.
A Boolean is an expression of truthfulness.
An integer is a number and only related to a Boolean value with 
regards to the hardware it's implemented on.

Why is 0 supposed to be false ? Why is 1 supposed to be true ? 
And why does D declare that 2 is not a valid Boolean while 2 
evaluates to true?

On the hardware level it makes sense to have a 'jump if zero' and 
'jump if not zero' instruction and implement it that way, but at 
the higher language level it doesn't - that's the point of being 
higher level.

It comes down to that because it's more 
performant/efficient/whatever than comparing to any other 
arbitrary value? Fair enough but that doesn't mean a Boolean 
false is in fact an integer.

I've done TTL electronics, too, and don't get the 5V argument and 
how that's supposed to be related to Boolean's supposed to be an 
integer 0..1 - for a 0 isn't plain 0.
Anything between 0V and 0.8V is low.
Anything from >0.8V to <2.0V is invalid and undefined behavior.
Anything between 2.0V and 5.0V is high.
Anything >5.0V will, exact threshold depends on the material, 
eventually destroy the IC.

So while we're at the business of translating numbers to bool why 
does it matter whether it's a float or an int? 0.0f is in the 
range of low.
 From that point of view I'd expect assigning 0.0 - 0.8 to bool is 
false and 2.0 - 5.0 is true and everything else blows up the 
compiler.

If it's not a type it would better be replaced by something like 
this:

alias Boolean = int;
immutable Boolean FALSE = 0;
immutable Boolean TRUE = ~FALSE;

or even better - the C way. Is it 0, or else...
This is completely predictable unlike the bool b = void; can be 
true and false at the same time.

The whole point of a Boolean data type is that it's something in 
its own right and decoupled from the implementation. Otherwise 
it's a redundancy.

But then, IMHO, Boolean is almost always best to be replaced by 
something more expressive like std.typecons.Flag

On that note if there's something that can be removed from the D 
language, it's bool.


More information about the Digitalmars-d mailing list