lua-users home
lua-l archive

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

I agree with John about the annoyance of this non-intuitive nil handling. And I do my set of tricks around it, as John pointed many of us do...

As a solution, how about just making #tbl etc. _fail_ if they are applied on a table that somehow is known to have holes in it. Then again, if that can be efficiently found out it wouldn't take much more to fallback to a less efficient hopping-over-holes algorithm for those failing cases. The annoyance is the unexpected behaviour, this certainly is one of Lua's "traps" (which every language has some).

To David:

Fixing the issue with #, unpack() etc. would not in my opinion hurt the general use of tables, in any way. It might cause these operators to run a bit slower, though (which is the real problem as far as I know?).


David Given kirjoitti 29.7.2007 kello 0:41:

Hash: SHA1

John Belmonte wrote:
Clearly they are not sparse as constructed.  Stepping back, a list is
generally defined a series of values. Lua nil is a first class value--
an incredibly useful one-- which should not render lists practically

The thing is, though, a Lua table is not a list. A table is a dictionary, a collection of key->value pairs. What you're referring to as a list is merely an Lua array: a table with a contiguous set of integer keys starting at 1.

The reason why the documentation refers to nil values as 'holes' is because an *unset* item in the dictionary is considered to have a value of nil. You remove a mapping by setting its value to nil. So if you try to define a table with a contiguous set of integer keys, etc, with one value as nil, then you've contradicted yourself: the keys are no longer contiguous because one item is unset. Since all the array operators are defined to *only* work on tables that
are valid arrays, then of course they'll fail.

I realise it's not an awful lot of help to you, but I think the key to your problem is that the abstraction you've based your code design around, the list, is not the same one that Lua is providing. So of course you're going to
run into problems.

What is it exactly you're trying to achieve?

- --
┌── ─── ───────────────────
│ "There does not now, nor will there ever, exist a programming language in │ which it is the least bit hard to write bad programs." --- Flon's Axiom
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla -