Should opEquals be removed from object? (Was: Re: How to check for null references)
Robert Fraser
fraserofthenight at gmail.com
Mon Aug 27 12:18:45 PDT 2007
This one seems to trip up a lot of new users. Is there some way (short of making it huge, red, and flashing) to make this somehow more obvious o the D webpage. I'm sure it trips up quite a few people who already know the rule, too (I don't do too much D work, and I've already been bitten once or twice).
I think the best way might be to issue a compiler error if an object doesn't have an opEquals() overload instead of providing a default implementation in object (like for all the other operator overloads). That would break a lot of existing code, though, but the compiler would help fix it rather than having it break silently.
Hans-Eric Grönlund Wrote:
> Thank you Tim (and the others that pointed this out)! I had a feeling there'd be a simple answer.
>
> Tim Healey Wrote:
>
> > Use 'is' to test for null:
> >
> > if ( o is null ) { ... }
> >
> > From http://www.digitalmars.com/d/expression.html#IdentityExpression :
> > For class objects, identity is defined as the object references are for
> > the same object. Null class objects can be compared with is.
> >
> > The run-time error you got is because the program was essentially
> > executing this:
> >
> > if ( o.opEquals( null ) ) { ... }
> >
> > This is intended behavior, though I'm not finding the right spec page to
> > link you to at the moment.
>
More information about the Digitalmars-d
mailing list