Deprecate implicit `int` to `bool` conversion for integer literals
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sun Nov 12 19:52:24 UTC 2017
On Sunday, November 12, 2017 19:13:00 Dmitry Olshansky via Digitalmars-d
wrote:
> On Sunday, 12 November 2017 at 16:00:28 UTC, Ola Fosheim Grøstad
>
> wrote:
> > On Sunday, 12 November 2017 at 13:34:50 UTC, Dmitry Olshansky
> >
> > wrote:
> >> if (a & (flag1 | flag2))
> >>
> >> to
> >>
> >> if ((a & (flag1 | flag2)) != 0)
> >>
> >> When the first is quite obvious.
> >
> > Just change the typing of the if-conditional to:
> >
> > if (boolean|integral) {…}
>
> Rather I recall that:
> if(expr)
> is considered to be
> if(cast(bool)expr)
>
> The latter to support user-defined types.
> So we are good.
Yes. In conditional expressions, you get an implicitly inserted cast to
bool. So, you have an implicit, explicit cast to bool (weird as that
sounds). If the implicit cast to integers to bool were removed (meaning
neither integer literals nor VRP allowed the conversion), then it would have
no effect on if statements or loops and whatnot. It would affect
overloading and other expressions. So, something like
bool a = 2 - 1;
or
auto foo(bool) {...}
foo(1);
wouldn't compile anymore. But something like
if(1)
would compile just fine, just like
if("str")
compiles just fine, but
auto foo(bool) {...}
foo("str");
does not.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list