Is empty array null?

Jonathan M Davis jmdavisProg at gmx.com
Mon Feb 27 18:36:46 PST 2012


On Monday, February 27, 2012 22:45:06 Pedro Lacerda wrote:
> So are a newly allocated array and a null one just the same thing?
> 
> int[] a = [], b = null;
> assert(a == b);
> assert(a.length == b.length);
> assert(a.ptr == a.ptr);
> 
> My code made distinction between empty and invalid collections, this mean
> that need to track the invalidation on another place?

In general, distinguishing between null and empty with arrays is a bad idea. 
There are a few cases where it can work (such as explicitly returning null 
from a function), but then you have to remember to use is and not ==.

If you really want to distinguish between null and empty without having to 
worry about all of the little details, then you can use std.typecons.Nullable.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list