lua-users home
lua-l archive

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


On Mon, Jun 1, 2015 at 11:59 AM, Andrew Starks <andrew.starks@trms.com> wrote:
>
>
> On Monday, June 1, 2015, Roberto Ierusalimschy <roberto@inf.puc-rio.br>
> wrote:
>>
>> > The right question is not whether a table is a sequence, but whether
>> > the default #tbl is deterministic for the particular table, i.e. whether
>> > the
>> > table has no holes. Being a sequence is merely a sufficient condition.
>> > The
>> > iterations `for i=1,#tbl` and `for i in ipairs(tbl)` both ignore
>> > non-integer keys.
>>
>> Sorry, but if you want to know whether '#tbl' has a reasonable meaning
>> (and is deterministic), then the right question is whether a table is
>> a sequence, because that is what the manual says:
>>
>>   Unless a __len metamethod is given, the length of a table t is only
>>   defined if the table is a sequence, that is, the set of its positive
>>   numerical keys is equal to {1..n} for some non-negative integer n. In
>>   that case, n is its length.
>>
>> The fact that the current internal implementation of '#' ignores
>> non-integer keys is an implementation detail.
>>
>> -- Roberto
>>
>
> Types! I asked this question last year and was told that I could depend on
> this behavior, but not by you or anyone else that is official. Nobody
> corrected the answer either.
>
> I depend upon this "implementation detail" quite often. I use it as a
> "sequence with properties."

"Sequence with properties" isn't an implementation detail as long as
your properties don't have non-integer numeric keys. Having
string-keyed (or table-keyed or lightuserdata-keyed etc.) tables
doesn't break the sequence-ness of it.

/s/ Adam