Distinguish between a null array and an empty array

bauss jj_1337 at live.dk
Sun May 24 12:29:23 UTC 2020


On Sunday, 24 May 2020 at 12:26:42 UTC, ag0aep6g wrote:
> 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);
> ----

Dang, that sucks there is no proper way and I would say that's a 
big flaw of D.

Because what I need it for is for some data serialization but if 
the value is an empty array then it should be present and if it's 
null then it should not be present.

Since null is used to say "ignore this" in the data serialization.

Oh well.


More information about the Digitalmars-d-learn mailing list