string <-> null/bool implicit conversion

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 21 07:12:52 PDT 2015


On Friday, 21 August 2015 at 10:50:40 UTC, Steven Schveighoffer 
wrote:
> On 8/20/15 11:15 PM, Jonathan M Davis wrote:
>> On Thursday, 20 August 2015 at 20:43:39 UTC, Steven 
>> Schveighoffer wrote:
>>> This makes me think you misunderstand what I am doing.
>>
>> If you care about whether an array is empty, you check whether 
>> its
>> length is 0 or you call empty (which checks whether the length 
>> is 0). If
>> you care about whether the array is null, you use "is null". I 
>> don't
>> understand what else you could possibly be doing. "!= null" is 
>> just
>> going to end up being equivalent to checking whether the 
>> length is 0,
>> because the elements won't be compared if the length is 0, but 
>> it gives
>> the false impression that you're checking whether the array is 
>> null -
>> hence why checking != null is a bad idea. What am I missing 
>> here?
>
> You're missing that null is not a pointer in this context, it's 
> an empty array. So checking whether the array is null (i.e. 
> empty) *is* what I'm doing, and it makes perfect sense to me. 
> The false impression is not one of my doing. I can't help your 
> C-based prejudices ;)

I know full-well that that's what you're doing, but that's not 
what your average programmer is going to expect. Anyone not 
intimately familiar with the ins and outs of D's arrays - or even 
a D programmer who's not paying really close attention - is 
likely to think that arr != null is checking whether the array is 
null - not whether the array is empty. And if I see code like 
that, I'm going to assume that the programmer who wrote the code 
really wanted was !is null but either made a mistake or didn't 
understand the difference between !is and != in this context.

Honestly, I think that writing code that uses != null is just as 
error-prone as using arrays in conditions directly. Certainly, 
it's just as confusing to many folks. Yes, it can work, but I 
think that it should be avoided, because it's often going to be 
misinterpreted by those reading the code, and anyone who reads 
the code who does understand the difference between is/!is and 
==/!= is not going to know whether the person who wrote the code 
understood the difference and used == or != on purpose or whether 
they screwed up and didn't use is or !is like they should have.

I would love to see == null and != null be illegal just as much 
as I think that it would be great to make it illegal to use 
arrays in conditions. It's the same problem.

- Jonathan M Davis


More information about the Digitalmars-d mailing list