lua-users home
lua-l archive

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

Le Fri, 13 Nov 2009 08:46:14 +0200,
steve donovan <> s'exprima ainsi:

> On Thu, Nov 12, 2009 at 5:52 PM, spir <> 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!

I'm sure mine are clearer but less efficient than yours!

> > * 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.

Ok, I will experiment with this. Do you mean name clashes; or maybe the lua equivalent of method lookup complications? Probably would opt for the explicit way... until I have an OO framework to make things clear.

> > * 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.

This is the point I don't get about metatables. How comes that lua does not "see" a method I _explicitely_ put on an object? It works when using obj.__tostring(obj) or obj:__tostring(); but when using print() or tostring() lua does not use __tostring.
If you have some minutes free, would someone explain in a few words how the metamethod magic works under the hood, so why __tostring does not get called without abracadabra?

> > * 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:
> (Disclaimer: I did it)
> (There is also stdlib, although IHMO it seems a bit disorganized)
> steve d.

Thank you Steve for your clear answers. And I'll have a look at your toolkit.

By the way, as questions about everyday tools seem to launch numerous answers, I thought at starting a "LuaToolkit" wiki page (or rather directory page) on the topic. The "portal" could point to specific pages dedicated to table output (human), table serialization, table content (iteration, membership...), and so on.
There, we could expose and comment various code snippets, evaluate them according to different criteria such as clarity, performance, etc. The main point beeing not to reinvent the wheel *worse*.
What do you think?
(There are numerous such pages and portalson the wiki, but rather about higher level and/or specific topics. This one would rather be for completing the everyday tools provided by the stdlib.)

* la vita e estrany *