Pointers vs. References
Bill Baxter
dnewsgroup at billbaxter.com
Wed Jun 13 11:43:41 PDT 2007
Ary Manzana wrote:
> Lionello Lunesu escribió:
>> That's not a bug.
>> '==' and '!=' result in a call to opEquals, which is likely to
>> segfault when one of the operands is null. You should use 'is' or in
>> your case '!is'.
>
> Can't writing
>
> x == null, x != null
>
> be rewritten by the compiler to
>
> x is null, x !is null
>
> ?
>
>
> I think the two firsts will always segfault. When would a user want to
> segfault her program? Or, better asked, when would a user want to do x
> == null?
>
> Regards,
> Ary
Why not have the compiler just rewrite all equality comparisons on class
objects?
x == y ===> (x is y || x.opEquals(y))
I mean you might as well check first that they aren't the same object,
because if they are then obviously they are equal too[1]. That would
make the behavior the norm and not a special case. I don't see how
doing one extra pointer comparison is going to significantly affect
performance.
But then I guess there's that bizzarro-business about opEquals returning
an int because that's "more efficient". So I guess someone is
hypersensitive about object equality operations being as fast as possible.
--bb
[1] Unless you're trying to implement some weirdo NaN like object which
is not equal to anything, including itself.
More information about the Digitalmars-d
mailing list