lua-users home
lua-l archive

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

On Fri, Aug 19, 2016 at 2:19 PM, Peter Aronoff <> wrote:
> Andrew Starks <> wrote:
>> Roberto Ierusalimschy <> wrote:
>> > I am not sure I understand what you are saying, but it is worth
>> > mentioning that sequences can have non-integer keys. For instance,
>> > {10, 20, 30, x=13, y=print} is a valid sequence in Lua.
>> And so is...
>> {10, 20, 30, x=13, y=print, [5] = 50}
>> I would say that the above table contains a sequence. By that definition,
>> any table with a non-nil value at index `1` contains a valid sequence.
> On the one hand, I hear you. It ‘contains a sequence’, but I wouldn’t be
> inclined to say that it *is* a valid sequence. The presence of integer keys
> with a hole would prevent me from saying it is a valid sequence. Maybe
> a bigger question is this: once the table looks like the last one here,
> *why* call it a sequence? (I’m trying to imagine a case where it would make
> sense to treat the first three items as a valid sequence and the rest
> otherwise. I’m failing.)
> Peter
> --
> We have not been faced with the need to satisfy someone else's
> requirements, and for this freedom we are grateful.
>     Dennis Ritchie and Ken Thompson, The UNIX Time-Sharing System

That is a design decision. If you need to check for holes and
therefore for validity, then you need to inspect every single key in
the table or verify validity on write, using metatables or some other

If you don't care, then you can live with the definition of a sequence being:

"Any table containing a non-nil value at index 1. The length of the
sequence is the number of non-nil keys from `1..n`."

Simple. Easy to test. If you need more, Lua has ways to provide it.

Andrew Starks