Change representation of dynamic arrays?

Janice Caron caron800 at googlemail.com
Sat Oct 20 04:59:29 PDT 2007


On 10/20/07, Jascha Wetzel <firstname at mainia.de> wrote:
> one can argue, that this way, empty arrays have an unambiguous
> representation (they are always null).

I will argue below that this is not a good thing.

Suppose I write a parser function which (say) parses an integer. It
would have a signature like

   bool parseInt(ref string s, out int n)

The idea is that if the function returns true, then the parsed int is
returned in n, and s's pointer is advanced past the bytes consumed.
However, I don't want s.ptr to be set to null, even if we consume all
the bytes. For example, I might want to do a pointer subtraction later
on. Or I might want to be able to extract the pointer because s was
merely a slice of a larger string.

For all sorts of reasons, allowing empty strings to have an address
does make sense.


> there is enough code like
> if ( str is null || str == "" )

I always write if (str.length == 0) to test for empty strings (and
arrays generally). That works just fine, always.



More information about the Digitalmars-d mailing list