Why is array truth tied to .ptr?

Bill Baxter dnewsgroup at billbaxter.com
Mon Dec 10 01:11:24 PST 2007


Janice Caron wrote:
> 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.

Agreed.  I might be ok with "zero" and "nonzero" but I'd rather have it 
be called empty() or isEmpty().  Anyway seems we agree there should be a 
default property like this.  It's kind of STL 101 that you should always 
call a container's 'empty' rather than checking 'size==0'.

--bb



More information about the Digitalmars-d mailing list