string <-> null/bool implicit conversion

Timon Gehr via Digitalmars-d digitalmars-d at puremagic.com
Thu Aug 20 12:59:53 PDT 2015


On 08/20/2015 09:54 PM, Steven Schveighoffer wrote:
> On 8/20/15 3:41 PM, Jonathan M Davis wrote:
>> On Thursday, 20 August 2015 at 17:50:11 UTC, Steven Schveighoffer wrote:
>>> if(arr != null)
>>
>> Definitely don't do that. IMHO, "== null and "!= null" should be
>> illegal. If you really want to check for null, then you need to use "is
>> null" or "!is null", whereas if you want to check that an array is
>> empty, check its length or call empty. By using "== null" or "!= null",
>> you tend to give the false impression that you're checking whether the
>> object or array is null - which is not what you're actually doing.
>
> On the contrary, checking if it's equal to null checks to see if it has
> the same elements as null. That's exactly what I would want.
> ...

It's about the /impression/, not about what it's actually doing. It 
clearly does the right thing.

> If I want to check the pointer, I check arr.ptr != null.
>
> I will note, I tend to have relatively no problem avoiding the
> conflation between null pointers and null arrays. That may not be true
> for most people, but I see them as two different things.
>
> -Steve

Sure, this is the same for me, but [] may be less confusing than null 
for most readers of the code and it's fewer keystrokes too.


More information about the Digitalmars-d mailing list