"is null" vs "== null"

Jari-Matti Mäkelä jmjmak at utu.fi.invalid
Mon Apr 3 12:44:09 PDT 2006


Lionello Lunesu wrote:
>> I'll venture to say that most people will expect that null == null, and at 
>> the very least, it seems that many (most?) people on the NG agree.
> 
> What about the general case: ptr == ptr (which includes the case where 
> ptr==null: null == null)? Should the compiler inhibit the call to opEquals 
> in the general case as well?
> 
> But settling this issue will not settle case where one of the arguments to 
> "==" is null. I saw some opEquals implementation somewhere that started with 
> the line "if (o is null) return false;", which seems silly, since if the 
> opEquals would have been called the other way around (b == a, instead of a 
> == b) then the program would have surely crashed (being a virtual call, 
> "this" must be valid to access the virtual function table). Right? "a==b" 
> should in any case be identical to "b==a".

That's right. Equality is a symmetric operation in mathematics. OTOH,
floating point values work correctly in D and D even has complex numbers
as a type. Thus as a "mathematical" :) language D should IMO definitely
keep the currently working equivalence semantics.

-- 
Jari-Matti



More information about the Digitalmars-d mailing list