Distinguish between a null array and an empty array

Nathan S. no.public.email at example.com
Tue May 26 02:23:32 UTC 2020


On Sunday, 24 May 2020 at 12:12:31 UTC, 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.

Yes you can tell: your `is null` check actually works, the part 
that doesn't work is that `int[] b = []` is initializing b to 
null.

Here's an example:

---
     int[0] emptyArray;
     int[] a = null; // a.ptr is null, a.length is 0
     int[] b = emptyArray[]; // b.ptr is non-null, b.length is 0

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

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



More information about the Digitalmars-d-learn mailing list