is ==

Neia Neutuladh neia at ikeran.org
Sat May 19 03:32:53 UTC 2018


On Saturday, 19 May 2018 at 01:48:38 UTC, Jonathan M Davis wrote:
> Actually, that runtime function has existed since before TDPL 
> came out in 2010. It even shows the implementation of the free 
> function opEquals (which at the time was in object_.d rather 
> than object.d). I'm not even sure that the error message was 
> added before the free function version of opEquals was. Maybe 
> when that error message was first introduced, it avoided a 
> segfault, but if so, it has been a _long_ time since that was 
> the case.

Good catch. I overly trusted git blame. The opEquals(Object, 
Object) function was added in February 2010, while the error 
message was added in March 2008.

> Of course, the most notable case where using == with null is a 
> terrible idea is dynamic arrays, and that's the case where the 
> compiler _doesn't_ complain. Using == with null and arrays is 
> always unclear about the programmer's intent and almost 
> certainly wasn't what the programmer intended. If the 
> programmer cares about null, they should use is. If they care 
> about lengnth, then that's what they should check. Checking 
> null with == is just a huge code smell.

I feel like the array == null version is more explicit about not 
allocating memory. However, I'm paranoid about whether that's 
going to check the pointer instead, so I mostly use array.length 
== 0 instead.


More information about the Digitalmars-d-learn mailing list