Checking if a string is null

Oskar Linde oskar.lindeREM at OVEgmail.com
Wed Jul 25 23:26:51 PDT 2007


Derek Parnell wrote:

> On Wed, 25 Jul 2007 19:01:57 +0200, Frits van Bommel wrote:
> 
>> Since null arrays have length 0, they *are* empty arrays :P.
> 
> Not in my world. I see that null arrays have no length. That is to say, the
> do not have any length, which is different from saying they have a length
> and that length is zero.

But that is not how T[] behaves in D. T[]s are of a dual slice/array 
nature with semantics closer to a slice than an array. That is something 
Walter's T[new] suggestion has a potential to remedy.

There is no difference between a "null" array and a slice starting at 
memory location null, 0 elements long. In my opinion, it would be quite 
strange for zero length slices to behave any differently if the starting 
position happens to be null.

There is a very easy way to get the behavior you want BTW:

class Array(T) { ... } :)

>>> All that I would like changed is for the compare, in the case of length 
>>> == 0, to check the data pointers, eg.
>>>
>>>  > int opEquals(T)(T[] u, T[] v) {
>>>  >     if (u.length != v.length) return false;
>>>       if (u.length == 0) return (u.ptr == v.ptr);
>>>  >     for (size_t i = 0; i < u.length; i++) {
>>>  >         if (u[i] != v[i]) return false;
>>>  >     }
>>>  >     return true;
>>>  > }
>>>
>>> This should mean "" == "" but not "" == null, likewise null == null but 
>>> not null == "".

This would mean that "two arrays are equal if all elements are equal" 
would no longer hold. (Consider two zero length slices at arbitrary 
memory location, neither of them null).

-- 
Oskar


More information about the Digitalmars-d-learn mailing list