lua-users home
lua-l archive

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


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David Burgess wrote:
[...]
> I agree with point that John makes. I also agree that using false
> instead of nil goes someway to resolving the issue. I have even
> used lightuserdata (char*)NULL to represent non-existence in
> tables. However, I think Lua should move to providing a length
> (#) operator that always "works" for arrays and iteration that always
> "works" for arrays (sparse or otherwise). Is is tough to explain
> the current behaviour to the novice Lua user.

This is rather against the Lua Way, which is to provide the fundamental tools
necessary to achieve the functionality, but leave it up to the user to use the
tools to achieve the desired goal.

table as it stands provides an efficient associative map that's also efficient
for the special case when you're using the table as an array. That doesn't
make it any less of a map, though, so you still have be aware of the
semantics. Change the way table works and you end up changing the core feature
of the language --- very dangerous.

If you *really* want a true array, wrap it in a structure that keeps manual
track of the length, or just do 'unset = {}' and use 'unset' as a placeholder.

It's all very well saying this behaviour is confusing to new users, but
efficient use of tables is an absolutely key aspect to writing Lua programs.
It's something that just has to be learnt. (See signature!)

(Although I will admit that this is far too easy to get it wrong, and break an
array and have to suffer the weird bugs you get that way; but rather than
change the language, I'd much rather have debugging tools to let you know when
you get it wrong. A strict mode, for example, that makes the array-aware tools
check to make sure that the table they're working on is a valid array...

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGq+IRf9E0noFvlzgRAmN1AKCIJt8Jqw/0+3u3ZE3j2KZYkaEzigCgtUck
r9y1upe5pQKZQ4CwLSiShLg=
=+eoQ
-----END PGP SIGNATURE-----