DIP 1015--removal of implicit conversion from integer and character literals to bool--Community Review Round 1

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Jun 21 21:49:39 UTC 2018


On Thursday, June 21, 2018 17:23:12 Steven Schveighoffer via Digitalmars-d 
wrote:
> On 6/21/18 3:29 PM, Jonathan M Davis wrote:
> > On Thursday, June 21, 2018 18:56:39 Francesco Mecca via Digitalmars-d 
wrote:
> >> On Thursday, 21 June 2018 at 17:11:07 UTC, Steven Schveighoffer
> >>
> >> wrote:
> >>> On 6/20/18 4:16 AM, Mike Parker wrote:
> >>>> [...]
> >>>
> >>> I agree with everything, but one thing that is not specified
> >>> here is when integers are used as conditionals.
> >>>
> >>> In other words, this still has to compile:
> >>>
> >>> if(1) ...
> >>>
> >>> enum a = 1;
> >>>
> >>> if(a) ...
> >>>
> >>> I can see this somehow getting caught up in the "implicit
> >>> conversion to bool", so there should be a section to address
> >>> this.
> >>
> >> I agree and after reading the DIP I am still confused if we are
> >> allowed to write while(1) (and similar conditionals) anymore.
> >
> > while(1)
> >
> > is not technically an implicit conversion. It's an implicit, explicit
> > conversion, which sounds kind of dumb, but it is technically what
> > happens, since when you write a conditional, the compiler inserts an
> > explicit cast.
> I'm not so sure this is the case for integer literals (literally, I'm
> not sure). It's certainly the case for custom types. The compiler
> handles things with builtin types specially. See for instance foreach on
> ranges vs. arrays.
>
> It might be that this actually is an implicit conversion (it's a literal
> after all). In that case, what I DON'T want to see after this proposal
> is accepted is an explanation like "well, why wouldn't you just use
> while(true)? It's the same thing".
>
> It's why I think the document needs clarification.

In the general case, it has to be an explicit conversion even with literals,
because stuff like

while(2)

works. So, I think that it's quite clear that simply getting rid of the
implicit conversion of 1 to true can't affect conditionals (though it's
certainly possible that the current implementation would run into issues
depending on whether the compiler always literally inserts an explicit cast
or whether it just does so when it would be required). It simply doesn't
make sense that while(1) would fail to work when while(2) continued to work
just fine. The semantics of conditionals require that while(1) would
continue to work even if the implicit conversion to bool is removed. As
such, I think that it's pretty clear what the DIP has to mean if you
understand the semantics of conditionals.

But that being said, I agree that the DIP should be clarified. Plenty of
folks don't understand conditionals properly and would mistakingly come to
the conclusion that this was banning while(1), and there's no reason not to
just be explicit about it for the sake of clarity. We don't want folks to be
confused about it, and it should be fairly easy to make the DIP clear on the
matter.

- Jonathan M Davis



More information about the Digitalmars-d mailing list