DIP 1015--Deprecation of Implicit Conversion of Int. & Char. Literals to bool--Formal Assement

Steven Schveighoffer schveiguy at gmail.com
Tue Nov 13 16:41:34 UTC 2018


On 11/13/18 11:26 AM, Chris M. wrote:
> On Monday, 12 November 2018 at 09:45:14 UTC, Mike Parker wrote:
>> DIP 1015, "Deprecation and removal of implicit conversion from integer 
>> and character literals to bool, has been rejected, primarily on the 
>> grounds that it is factually incorrect in treating bool as a type 
>> distinct from other integral types.
>>
>> The TL;DR is that the DIP is trying to change behavior that is working 
>> as intended.
>>
>> From Example A in the DIP:
>>
>>     bool b = 1;
>>
>> This works because bool is a "small integral" with a range of 0..1. 
>> The current behavior is consistent with all other integrals.
>>
>> From Example B in the DIP:
>>
>> ```
>> int f(bool b) { return 1; }
>> int f(int i) { return 2; }
>>
>> enum E : int
>> {
>>     a = 0,
>>     b = 1,
>>     c = 2,
>> }
>> ```
>>
>> Here, f(a) and f(b) call the bool overload, while f(c) calls the int 
>> version. This works because D selects the overload with the tightest 
>> conversion. This behavior is consistent across all integral types. 
>> Replace bool with ubyte and f(a), f(b) would both call the ubyte 
>> version. The same holds for the DIP's Example C.
>>
>> Walter and Andrei left the door open to change the overload behavior 
>> for *all* integral types, with the caveat that it's a huge hurdle for 
>> such a DIP to be accepted. It would need a compelling argument.
>>
>> You can read a few more details in the summary I appended to the DIP:
>>
>> https://github.com/dlang/DIPs/blob/master/DIPs/rejected/DIP1015.md#formal-assessment 
>>
>>
>> Thanks to Mike Franklin for sticking with the process to the end.
> 
> I was going to write something up about how you can't do arithmetic on 
> bool types therefore they aren't integral, but I tested and realized D 
> allows this (i.e. bool + bool, bool * bool). Still that seems 
> nonsensical, so I guess my question what is the definition of an 
> integral type and how does bool fit?

What it's doing is promoting false to an integer 0 and true to an 
integer 1. These work just like all the other integer promotion rules.

Interestingly enough, since bool can only be promoted to 0 or 1, bool * 
bool can be assigned back to a bool, but bool + bool can't be assigned 
back to a bool unless you cast. But don't expect the addition to behave 
as other integers do, as true + true == 2, which then casts to true.

-Steve


More information about the Digitalmars-d-announce mailing list