lua-users home
lua-l archive

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


On Thu, Nov 12, 2009 at 5:52 PM, spir <denis.spir@free.fr> wrote:
> * Is there a standard way to iterate _only_ on keys / values of a table; or items of table used as a plain array? (have written iterators for that, but would prefere to use std way)

None; you have done what any Lua programmer would do in this case!

> * Stdlib operations on strings and files can be written obj:op(). But this does not seem to work for tables. Am I right? Is there a reason for this? Is it possible to make the syntax uniform by assigning stdlib funcs defined on "table" to the type "table"?

It's possible that every new table gets a metatable pointing to
'table', so list:concat() etc. The Lua fork Idle does this. But (as
has been pointed out before) it leads to confusion, because very often
we define our own metatables.  One idea is to define T() so that
T{10,20,30} gives a table with the table functions as methods; this is
at least explicit.

> * Is there a way to make lua _implicitely_ use a "method" for print-ing? (thought __tostring was for this, but I can't make it work)

If the table (or metadata) has a metatable which contains __tostring,
then that does the trick, since print() uses tostring() which will use
the metamethod __tostring.

> * Is there a standard way to copy (clone) lua objects --especially tables?

Again, no.

Naturally, there have been attempts to provide libraries of these
things, for instance:

http://penlight.luaforge.net/

(Disclaimer: I did it)

(There is also stdlib, although IHMO it seems a bit disorganized)

steve d.