So, null arrays and empty arrays are always the same, except for an empty string, which is a valid non-nill array of characters with length 0, right?

On Mon, May 14, 2012 at 2:24 PM, simendsjo <simendsjo@gmail.com> wrote:
On Mon, 14 May 2012 12:08:17 +0200, Gor Gyolchanyan <gor.f.gyolchanyan@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 == "");
}



--
Bye,
Gor Gyolchanyan.