Is empty array null?

Jesse Phillips jessekphillips+D at gmail.com
Tue Feb 28 02:20:16 PST 2012


On Tuesday, 28 February 2012 at 08:38:10 UTC, Mikael Lindsten 
wrote:
> Coming from the Java/C# world, not distinguishing between an 
> empty array
> and null feels strange to me. Is this so for strings as well? 
> ...and in
> Pedros example, if you assign null to b and then try to access 
> b.length,
> don't you get a NullPointerException? What am I missing?
>
>
> / Mikael

string is an array, alias immutable(char)[], so the same rules 
apply.

There is no NullPointerException, it is a segfault/Access 
Violation/Bus Error.

An empty array and a null array are equal. And this definition 
does work well.

You can distinguish between an empty array and a null array by 
using the 'is' operator. What is pointed out here is that an 
empty array literal, [], is null while an empty array is not. 
This behavior is easy to explain in terms sf why, but may not be 
a good choice for sake of consistency.


More information about the Digitalmars-d-learn mailing list