Distinguish between a null array and an empty array

ag0aep6g anonymous at example.com
Sun May 24 12:26:42 UTC 2020


On 24.05.20 14:12, 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.

No way. `null` and `[]` are the same thing for arrays. (Ulike `""` for 
strings which has a non-null `.ptr`.)

You can distinguish `null` from other (non-null, non-`[]`) empty arrays. 
For example:

----
int[] b = [1, 2, 3];
b = b[0 .. 0];

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


More information about the Digitalmars-d-learn mailing list