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 19:29:03 UTC 2018
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.
> >
> > -Steve
>
> 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.
So, when you write something like
if(foo)
it becomes
if(cast(bool)foo)
So, while you don't explicitly put the cast there, semantically, that's
what's happening, whereas actual implicit conversions don't use casts.
That's why you can do typically stuff like
if(myObject)
but can't do
bool b = myObject;
Any type that can be explicitly cast to bool can therefore be used in a
conditional, but only a type with an implicit conversion can be assigned to
a bool, passed to a function that accepts bool, or be used in any other
situation where an actual bool is required.
So, since the DIP just talks about altering implicit conversions, I think
that that it's actually pretty clear that it does not affect conditionals.
That being said, it wouldn't be a bad idea to explicitly state in there that
it has no effect on code like conditionals where explicit casts are inserted
by the compiler.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list