The more interesting question

FeepingCreature default_357-line at yahoo.de
Mon May 14 03:42:49 PDT 2012


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.


More information about the Digitalmars-d mailing list