DMD can implicitly convert class pointer to the bool. Is it bug or terrible feature?

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 24 17:53:56 PST 2013


On Sunday, November 24, 2013 18:38:19 Ary Borenszweig wrote:
> On 11/24/13 11:18 AM, ilya-stromberg wrote:
> > On Sunday, 24 November 2013 at 14:12:18 UTC, Maxim Fomin wrote:
> >> void* ptr;
> >> if(ptr)
> >> 
> >> was a shortcut for 'if(ptr != NULL)' probably since C was created.
> > 
> > Small code change:
> > 
> > import std.stdio;
> > 
> > class Foo
> > {
> > }
> > 
> > void main()
> > {
> > 
> >      Foo f;
> >      
> >      if(f == null)
> >      {
> >      
> >          writeln("f is true");
> >      
> >      }
> >      
> >      if(f != null)
> >      {
> >      
> >          writeln("f is false");
> >      
> >      }
> > 
> > }
> > 
> > DMD output:
> > 
> > Error: use 'is' instead of '==' when comparing with null
> > Error: use '!is' instead of '!=' when comparing with null
> 
> Ugh, if the compiler disallows comparison of reference with "==" and
> "!=" and tells you to use "is" and "!is", can't compiler just allow you
> to write "==" and understand it as "is"? What's the big deal?

Likely because using == indicates a misunderstanding of what == does and how 
null works.  Conceptually, == and is are two very different operations, and 
they shouldn't be mixed up. Also, using == null incurs some extra overhead, 
because it ends up calling opEquals.

- Jonathan M Davis


More information about the Digitalmars-d mailing list