lua-users home
lua-l archive

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

Whilst reading the Lua 5.0 reference I was somewhat surprised to
discover that the table iteration idion:

for k,v in t do

is no longer supported, which is to say that this syntax has been
supplanted by the functional iterator idiom.

But in actual fact it all works.  The above syntax does in fact iterate
over the keys of the table t (if t is a table), but this isn't mentioned
in the reference manual.  From the manual I get the impression that I'm
supposed to use

for k,v in pairs(t) do

ok, fair enough.

So the behaviour of "for k,v in t do" varies depending on whether t is a
table or a function - a fact not mentioned in the manual and it should

What if t has a metatable with a __call method?  No change, still
treated like a table, contradicting the reference manual.

Can we have a clarification in the manual please?

Something suitable would be:

  There is a concession made for iterating over tables in a manner
  compatible with the Lua 4.0 syntax:

  If the for loop has the form:
    for var_1, ..., var_n in exp do block end
  (that is, just a single expression in place of the explist) and if exp
  evaluates to a table then it is equivalent to a for loop of the form:
    for var_1, ..., var_n in next, exp do block end

  Normally this will iterate over the keys of the table exp; var_1 will
  be assigned successive keys, var_2 will be assigned the corresponding
  table entry, and any remaining var_i variables will be nil.  The
  "next" in the above code is a reference to the global variable next;
  this variable is normally used by the base library but can obviously
  be overridden.  Any other variable in scope called next is ignored.

Possibly the details in this suggested text are wrong, I only had a
quick look at the implementation; I looked at lvm.c but not lparser.c.