DIP 1015--Deprecation of Implicit Conversion of Int. & Char. Literals to bool--Formal Assement
Jacob Carlborg
doob at me.com
Mon Nov 12 11:05:55 UTC 2018
On 2018-11-12 10:45, 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.
Why is that? Is it because "a" and "b" are enum members? I mean, "E" is
typed as an int. If I pass an integer literal directly to the function
it doesn't behave like that. Example:
import std.stdio;
void foo(int a)
{
writeln("int");
}
void foo(bool a)
{
writeln("bool");
}
void main()
{
foo(0);
foo(1);
foo(2);
}
The above example prints "int" three times. The bool overload is not
called. Seems like the enum is treated specially.
--
/Jacob Carlborg
More information about the Digitalmars-d-announce
mailing list