lua-users home
lua-l archive

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


> AFAICS, the numeric for is simply a syntactical convenience which allows
> loops like this:
The generic for loop is also a syntactical convenience (although
generic for and numeric for both use dedicated opcodes in the VM
rather than the ones used by while/until loops).

> I'm not sure why you'd want or need to implement a numeric for using a
> generic for (as you seem to want to do), though you can if you wish (just
> not in the way you propose).
>
It simply seems inconsistent that a numeric for loop is the only place
in Lua where a function call or vararg at the end of the expression
list is not expanded to the number of returned results;
a,b = f()
t = {a, b, f()}
f(a, b, f())
for a,b in f() do
for a = b, f() do -- The second return value of f(), if there is one,
is discarded

>
> Note (Blue PiL pp.32-33) the additional and not unimportant semantic
> distinction that in the numeric for, the start,stop,step values must
> evaluate to numbers and are evaluated just once, before the loop starts.
> In generic for, the iterator function is called each time round the loop
> until it returns nil.
>
Just like the numeric for must give a start, stop, step and these are
evaluated once, in the generic for, the explist is evalulated just one
and must evaluate to an iterator function, a state, and an initial
control variable.