At least we could make an empty string a null array of characters for consistency. How many times did anyone use the feature of string literals being null-terminated?<br><br><div class="gmail_quote">On Mon, May 14, 2012 at 8:56 PM, Jonathan M Davis <span dir="ltr"><<a href="mailto:jmdavisProg@gmx.com" target="_blank">jmdavisProg@gmx.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Monday, May 14, 2012 14:08:17 Gor Gyolchanyan wrote:<br>
> Hi! I have a small question:<br>
> Is the test for a null array equivalent to a test for zero-length array?<br>
> This is particularly interesting for strings.<br>
> For instance, I could return an empty string from a toString-like function<br>
> and the empty string would be printed, but If I returned a null string,<br>
> that would indicate, that there is no string representation and it would<br>
> cause some default string to be printed.<br>
> So, the question is, if a null array is any different from an empty array?<br>
<br>
</div></div>A null array is equal to an empty array.<br>
<br>
assert(null == []);<br>
assert(null == "");<br>
<br>
It's when you use is that things change. An array "is null" only if it's ptr<br>
property is null, which is true only for uninitialized arrays and [] (the<br>
compiler doesn't bother allocating memory for [], so it's basically the same<br>
as null). However, since "" is a string literal, it _does_ have memory<br>
allocated to it, so<br>
<br>
assert([] is null);<br>
assert("" !is null);<br>
<br>
So, it all comes down to the ptr property. An array is considered to have a<br>
length of 0 if its length property is 0 (which null arrays do). It's only<br>
considered to be null if its ptr property is null. == uses the length<br>
property, ignoring the ptr property as long as the lengths are equal (and<br>
checking each of the elements refered to by the ptr property if the lengths<br>
aren't equal), whereas is specifically checks whether the ptr properties of the<br>
two arrays are equal.<br>
<br>
Personally, I think that conflating null and empty like this is atrocious, but<br>
that's how the language works, and we're stuck with it.<br>
<span class="HOEnZb"><font color="#888888"><br>
- Jonathan M Davis<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br>