On 14-05-2012 19:16, Gor Gyolchanyan wrote:
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?
On Mon, May 14, 2012 at 8:56 PM, Jonathan M Davis <[email protected]
<mailto:[email protected]>> wrote:
On Monday, May 14, 2012 14:08:17 Gor Gyolchanyan 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?
A null array is equal to an empty array.
assert(null == []);
assert(null == "");
It's when you use is that things change. An array "is null" only if
it's ptr
property is null, which is true only for uninitialized arrays and []
(the
compiler doesn't bother allocating memory for [], so it's basically
the same
as null). However, since "" is a string literal, it _does_ have memory
allocated to it, so
assert([] is null);
assert("" !is null);
So, it all comes down to the ptr property. An array is considered to
have a
length of 0 if its length property is 0 (which null arrays do). It's
only
considered to be null if its ptr property is null. == uses the length
property, ignoring the ptr property as long as the lengths are equal
(and
checking each of the elements refered to by the ptr property if the
lengths
aren't equal), whereas is specifically checks whether the ptr
properties of the
two arrays are equal.
Personally, I think that conflating null and empty like this is
atrocious, but
that's how the language works, and we're stuck with it.
- Jonathan M Davis
--
Bye,
Gor Gyolchanyan.
See my other post in reply to your original thread.
--
- Alex