lua-users home
lua-l archive

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


2012/9/13 Egor Skriptunoff <egor.skriptunoff@gmail.com>:
> Why this simple code
>
> for i = 1, 5 do print(i) end
>
> cannot be written in more general form ?
>
> local function f() return 1, 5 end
> for i = f() do print(i) end
>

The syntax of the numeric "for" is:

for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end

I.e. the comma is part of the syntax, and
 exp ‘,’ exp [‘,’ exp] is not an expression list.

At no stage is "exp,exp" or "exp,exp,exp" assigned to
Name.  Every "exp" must be a number.  The third "exp"
may be omitted but it may not be nil.

The syntax for the generic "for" is:

for namelist in explist do block end |

Here, any commas you use belong to either explist
or namelist and explist may be (in fact, usually is)
a function.