Checking if a string is null

Bruno Medeiros brunodomedeiros+spam at com.gmail
Wed Jul 25 07:07:15 PDT 2007


Regan Heath wrote:
> Bruno Medeiros wrote:
>> Frits van Bommel wrote:
>>> Regan Heath wrote:
>>>> This all boils down to the empty vs null string debate where some 
>>>> people want to be able to distinguish between them and some see no 
>>>> point.
>>>>
>>>> I'm in the 'distinguishable' camp.  I can see the merit.  At the 
>>>> very least it should be consistent!
>>>
>>> They *are* distinguishable. That's why above code returns different 
>>> results for the 'is' comparison...
>>>
>>
>> The .ptr of empty arrays may be different than the .ptr of null 
>> arrays, but they are conceptually the same, and thus not safely 
>> distinguishable.
>> Example:
>>     writefln("" is null); // false
>>     writefln("".dup is null); // true
>>
>> "".ptr is not null, but "".dup.ptr is null. Such duplication is 
>> correct, because empty arrays are conceptually the same as null 
>> arrays, and trying to use .ptr do distinguish them is unsafe, 
>> implementation-depedendent behavior (aka a program error).
> 
> Ick.  IMO "".dup should allocate 1 byte of memory, set it to '\0' and 
> create a reference to it with length of 0.
> 
> What do you mean by "empty arrays are conceptually the same as null 
> arrays"?
> 

I meant that in current D they are semantically the same. (I should have 
used those words)

> To me null arrays (non-existant) and "" arrays (empty) are conceptually 
> different.  null indicates the array does not exist (no set at all), "" 
> indicates it does but contains no items (an empty set).
> 
> Regan

I know, and I agree, don't you recall the V2 string discussion:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=55388

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D


More information about the Digitalmars-d-learn mailing list