arrays: if(null == [ ])

Jonathan M Davis jmdavisProg at gmx.com
Mon May 14 13:12:51 PDT 2012


On Monday, May 14, 2012 21:16:21 Gor Gyolchanyan wrote:
> At least we could make an empty string a null array of characters for
> consistency.

It's completely consintent. If there is memory allocated for the array, then 
it's ptr property is non-null, and then the array is non-null per the is 
operator. If there is no memory allocated for the array, then it's ptr 
property is null, and the array is null per the is operator.

It's quite possible to have non-string arrays be non-null with a length of 0. 
It just takes more work. e.g.

auto a = new int[](1);
a.length = 0;

And it would actually be really annoying if "" were treated the same as [], 
because it becomes harder to differentiate between the empty string and a null 
string in the few cases where you care. If anything, the fact that [] doesn't 
allocate is _more_ annoying as far as that goes. But it's more efficient that 
way, so that's why [] is null.

> How many times did anyone use the feature of string literals
> being null-terminated?

It gets used all the time when people call C functions and is a _really_ good 
reason why making assert("" is null) true would be a _really_ bad idea.

- Jonathan M Davis


P.S. Please don't top-post. It's bad etiquette and makes posts harder to follow.


More information about the Digitalmars-d mailing list