lua-users home
lua-l archive

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


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

Fernando P. García wrote:
> Thank you, how can I "fix" that in pure lua?

Do this:

setmetatable(t, {__index=table})

...for each and every table that you want to act like this. You can't do
it globally.

However, this will cause you problems down the line, because:

(a) if you want to set a different metatable on one of your tables, that
table will suddenly start behaving differently;

(b) your table's contents will suddenly become pulluted with the
contents of the table utility library;

For example:

t:sort()
t["sart"] = 1
t["sert"] = 1
t["sirt"] = 1
t["sort"] = 1
t["surt"] = 1
t:sort() -- crash!

If you want to deal with object-oriented data structures in Lua, your
best bet is to build your own rather than trying to extend tables like
this --- less problematic. (This is one of the places where Lua's
non-object-oriented philosophy really shows through.)

- --
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "There is nothing in the world so dangerous --- and I mean *nothing*
│ --- as a children's story that happens to be true." --- Master Li Kao,
│ _The Bridge of Birds_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iD8DBQFK5NhKf9E0noFvlzgRAuuTAKCJDBwOlzpNhgkK1kO/b5hHbSw5ZACg2E95
HnwYDeDRI6bp3O74BJIA2+s=
=5mPL
-----END PGP SIGNATURE-----