lua-users home
lua-l archive

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


On 2012-12-06 9:04 PM, "Javier Guerra Giraldez" <javier@guerrag.com> wrote:
>
> On Thu, Dec 6, 2012 at 6:56 PM, Wolfgang Pupp <wolfgang.pupp@gmail.com> wrote:
> > It'd be like giving the language a concept of "traversing a table". By
> > default, this traversal would behave like pairs (the *only* sensible
> > choice).
>
> Lua 4.0 was like that.
>
> Lua 5.0 removed the magic, making it smaller and more flexible.
>
> --
> Javier
>

_javascript_ also uses this behaviour, and it never fails to cause me problems, because I keep forgetting that it will give me not only the numeric keys of an array, but the "length" field as well. pairs may be the only sensible default iterator if your table has mostly string keys, but ipairs is the only sensible iterator for numeric arrays... and since a table can be either (or both!), it seems there's no sensible default at all.

I feel like this thread is getting confused between two separate ideas - the "for k,v in t" syntax, and having a default metatable for new tables (which is something I had considered a while ago, but not known enough about Lua's innards to attempt).

While I'm not wild about the idea of changing table iteration, I feel like having "table" as a default metatable for tables may be a good idea.