Distinguish between a null array and an empty array

Steven Schveighoffer schveiguy at gmail.com
Tue May 26 16:11:30 UTC 2020


On 5/24/20 8:12 AM, bauss wrote:
> Is there a way to do that?
> 
> Since the following are both true:
> 
> int[] a = null;
> int[] b = [];
> 
> assert(a is null);
> assert(!a.length);
> 
> assert(b is null);
> assert(!b.length);
> 
> What I would like is to tell that b is an empty array and a is a null 
> array.

The issue is simply that [] is the same as null.

Try this:

T[] emptyArr(T)() @trusted
{
    T* p = null;
    return p[1 .. 1];
}


int[] b = emptyArr!int;

assert(b !is null);
assert(!b.length);

-Steve


More information about the Digitalmars-d-learn mailing list