arrays: if(null == [ ])

simendsjo simendsjo at gmail.com
Mon May 14 03:24:35 PDT 2012


On Mon, 14 May 2012 12:08:17 +0200, Gor Gyolchanyan  
<gor.f.gyolchanyan at gmail.com> wrote:

> Hi! I have a small question:
> Is the test for a null array equivalent to a test for zero-length array?
> This is particularly interesting for strings.
> For instance, I could return an empty string from a toString-like  
> function
> and the empty string would be printed, but If I returned a null string,
> that would indicate, that there is no string representation and it would
> cause some default string to be printed.
> So, the question is, if a null array is any different from an empty  
> array?
>

This passes. null and [] is a null string, but "" gives a non-null string.  
Tested on dmd-head and 2.059.

void main() {
     string s1 = null;
     assert(s1 is null);
     assert(s1.length == 0);
     assert(s1.ptr is null);
     assert(s1 == []);
     assert(s1 == "");

     string s2 = [];
     assert(s2 is null);
     assert(s2.length == 0);
     assert(s2.ptr is null);
     assert(s2 == []);
     assert(s2 == "");

     string s3 = "";
     assert(s3 !is null);
     assert(s3.length == 0);
     assert(s3.ptr !is null);
     assert(s3 == []);
     assert(s3 == "");
}


More information about the Digitalmars-d mailing list