lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


chris <csrl@gmx.com> writes:
>> Yes (but of course, it's the usual case that one does know how one's
>> data structures are structured ... :)
>
> Not in the context I'm working within.  The issue nearly becomes a
> mute point if one knows the data structure, and so wouldn't be under
> discussion.

Er, what's the point of the discussion, anyway?

I mean, sure, if your particular code needs to deal with tables of
unknown composition, then yeah, you'll either have to track the length
explicitly in some way, or calculate it yourself.

But that doesn't make the # operator any less useful for the huge amount
of code where tables of known properties _are_ used... 
The original post, I presume,  was because you misunderstood the scope
of the # operator, and thought it had a bug.  That misunderstanding was
clarified, so I'm not sure where this is going ...


>> > If you know it is a sequence, that means you could have already
>> > tracked the length manually
>> 
>> Sure, you _could_, but why would you want to, when the # operator
>> does the right thing, and is more convenient (and in many cases more
>> efficient)?
>
> Because one already has to track the length in the case that an index
> might be assigned nil.  As such there is no benefit to having the
> efficiencies of the length operator if that is ever a possibility.

For your use-case, maybe; but there's an awful lot of code where it does
have a benefit.

> it makes better sense to expose the sequence length through a
> table.len() function to avoid this type of confusion.

Maybe (I dunno) it would be good to also habe a table.len function,
which does a slow but reliable count for all types of tables, but of
course the # operator still has a valuable place.

-miles

-- 
Generous, adj. Originally this word meant noble by birth and was rightly
applied to a great multitude of persons. It now means noble by nature and is
taking a bit of a rest.