Checking if a string is null

Regan Heath regan at netmail.co.nz
Wed Jul 25 07:30:10 PDT 2007


Bruno Medeiros wrote:
> Regan Heath wrote:
>>>> So, "" is < and == null!?
>>>> and <=,== but not >=!?
>>>>
>>>
>>> You didn't update all writefln's :)
>>
>> <hangs head in shame> What can I say, I'm having a bad morning.
>>
>>> Anyway, it feels like an undefined area in the language. Do the specs
>>> say anything about how exactly arrays/strings/delegates should compare
>>> to null? It seems to be more than comparing the pointer part of the
>>> structs.
>>
>> Not that I can find.  The array page does say:
>>
>> "Strings can be copied, compared, concatenated, and appended:"
>> ...
>> "with the obvious semantics."
>>
>> but not much more on the topic.  Under "Array Initialization" we see:
>>
>>     * Pointers are initialized to null.
>>     ..
>>     * Dynamic arrays are initialized to having 0 elements.
>>     ..
>>
>> Which does not state that an array will be initialised to "null" but 
>> rather to something with 0 elements.
>>
> 
> It's in
> http://www.digitalmars.com/d/expression.html#IdentityExpression
> "For static and dynamic arrays, identity is defined as referring to the 
> same array elements"

And under "Equality Expressions" we have:
"For static and dynamic arrays, equality is defined as the lengths of 
the arrays matching, and all the elements are equal."

Which is exactly what we see in the compare function, so it's following 
the spec.

But, this means "" compares equal to null and vice-versa which is 
something I think we want to change.

I am a little puzzled by the fact that:
"Identity Expressions" include ("is", "!is")
"Equality Expressions" include ("==", "!=", "is", "!is")
Why do "is" and "!is" exist in both equality and identity?

> But in current D empty arrays can have a null identity (even if they 
> don't allway have), so you can't use 'is' to try do distinguish null 
> arrays from empty arrays. Thus effectively they are semantically the 
> same in current D.

I understand what you're saying now.

Given that null and "" have different ptr values, they therefore "refer 
to different array elements" and "is" should distinguish them.

But, in current D implementation the .dup function isn't distinguishing 
the cases and is instead duplicating "" resulting in null and therefore 
preventing further distinction of the 2 cases.

This is similar to the behaviour where setting length to 0 used to free 
the data pointer, turning an empty array into a null one.

So, this definately needs fixing I reckon.

Regan



More information about the Digitalmars-d mailing list