The more interesting question

deadalnix deadalnix at gmail.com
Mon May 14 04:53:08 PDT 2012


Le 14/05/2012 12:42, FeepingCreature a écrit :
> On 05/14/12 12:08, 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?
>>
>> --
>> Bye,
>> Gor Gyolchanyan.
>
> The more interesting question, imo, is how the behavior of 'if (string)' (ie. bool conversion) should be defined. To my knowledge,
> it checks for ptr, which can lead to some confusion since "" == null, 'if ("")' is true but 'if (null)' is false!
>
> I think this behavior is still best though, for the simple reason that people think (or ought to think) of arrays as "pointers
> with length", so it makes sense that 'if (string)' tests the pointer. It's not intuitively obvious if you consider strings as sequences
> of characters, but it's obvious if you consider them as D arrays.

A good solution would be to set the pointer to 0 when the length is set 
to 0.


More information about the Digitalmars-d mailing list