+[JH-LUA-ITER]: Self-iterating Objects Powerpatch (see §2.4, +§3.3.5, §8).
rawset
to do the assignment.)
++
for items in explist do
. This event happens when the first and only non-nil
+item in explist
is not a function (or closure). If present, the
+metamethod is called with the item as its only argument. The return values replace
+the explist
and are used to perform the iteration in the usual
+way (see §3.3.5). Therefore, the metamethod should have the
+same signature as the pairs
global function.
+
func(args)
.
This event happens when Lua tries to call a non-function value
(that is, func
is not a function).
The metamethod is looked up in func
.
@@ -1663,13 +1682,23 @@
stopping when this new value is nil.
The generic for loop has the following syntax:
stat ::= for namelist in explist do block end namelist ::= Name {‘,’ Name} -
+ + +
+[JH-LUA-ITER]: As an alternative to an interator function, a table or userdata
+may be specified as the first and only expression in explist
. In this case a metamethod
+__iter
, if present, is called with the table or userdata as the only parameter. The first three
+return parameters replace the explist
for the iteration. In the case of a table, if a
+metamethod is not present an iteration method functionally equavalent to pairs
is used by default.
+
+
+ A for statement like
for var_1, ···, var_n in explist do block end
is equivalent to the code:
@@ -10697,12 +10732,25 @@
Use their equivalent over lua_Integer
with a type cast
(or, when possible, use lua_Integer
in your code).
for
is specified to operate over 'iterator functions'
+but in practice any callable is accepted allowing tables or userdata with __call
metamethods
+to be used. The powerpatch (see §3.3.5) restricts iterator functions to actual
+functions (or closures) unless there are additional values between in and do.
+