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

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 12 19:10:42 UTC 2018


On 11/12/18 4:45 AM, 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.

But it's not consistent:

void isItAnInteger(T, bool makeCompile = false)() // works for all integers
{
    T val = T.min; // I was surprised these work for bool
    val = T.max;
    static if(!makeCompile)
    {
        long x = -val; // Error: operation not allowed on bool b
        ++val; // Error: operation not allowed on bool val += 1
        val += 1; // same error
    }
    val = cast(T)(T.max + 1);
    assert(val == val.min); // error for bool, true + 1 == 2, but 
cast(bool)2 truncates to true, not false.
}

void main()
{
     import std.meta;
     static foreach(T; AliasSeq!(int, uint, long, ulong, short, ushort, 
byte, ubyte))
     {
         isItAnInteger!T();
     }

     // switch second parameter to false to see compiler errors.
     isItAnInteger!(bool, true)();
}

If you have the makeCompile flag set to true, then it asserts for bool, 
but nothing else.

-Steve


More information about the Digitalmars-d-announce mailing list