Why is array truth tied to .ptr?

Daniel Keep daniel.keep.lists at gmail.com
Mon Dec 10 00:35:46 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.

I never suggested z and nz as standard names; that would be a ridiculous
proposition, as I doubt many people would be able to work out what they
meant at first glance.  I use them because

a. they're shorter than isEmpty and !isEmpty,

b. I'm lazy when it comes to typing and

c. because they're actually named after jnz and jz from assembler, which
I don't expect *anyone* to pick up on.

I'm not a big fan of 'isEmpty' because I think it's too long, *and* it
requires me to hit shift [1].  'empty' would be OK, but then that's both
a noun and a verb, which leads to ambiguity over exactly what it does. [2]

All things considered, 'isEmpty' is probably the best choice for
inclusion as a standard property.  That said, I'll keep my lovely .z &
.nz properties.  :)

	-- Daniel

[1] I realise the irony of this, in that .z and .nz require me to type a
set of parens.  None the less, I find typing '.z()' much faster and
easier than '.isEmpty' due to the way my hands move as I type.

[2] Sometimes, I really wish '?' and '!' were valid characters in
identifiers, like in Ruby.



More information about the Digitalmars-d mailing list