Why is array truth tied to .ptr?

Janice Caron caron800 at googlemail.com
Mon Dec 10 00:21:25 PST 2007


On Dec 10, 2007 4:21 AM, Daniel Keep <daniel.keep.lists at gmail.com> wrote:
> I actually went and created a pseudo-property for just this purpose:
>
> /// Tests the array to see if it's of non-zero length.
> bool nz(T)(T arr) { return arr.length != 0; }
> /// Tests the array to see if it's of zero length.
> bool z(T)(T arr) { return arr.length == 0; }

What is relevant to this whole question is the fact the I recall
Walter saying he was thinking of changing the representation of an
array from struct { ptr, length } to struct { ptr, end }. I suggested
at that time that all arrays should be given an isEmpty property,
because isEmpty() is just a sensible idea for collections in general.

I have to say though, that I don't like the function names z() and
nz(). I don't equate "zero" with "zero length". To me, the only
meaning I could attribute to "is zero" would be "is full of zeroes".
That is, I would expect the array [ 0, 0, 0, 0 ] to pass a test called
z() and fail a test called nz(). That's why I'd suggest isEmpty() as
the preferred name.



More information about the Digitalmars-d mailing list