lua-users home
lua-l archive

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


On Thu, Sep 13, 2012 at 6:51 PM, Dimitris Papavasiliou
<dpapavas@gmail.com> wrote:
> Egor does have a point though.  Since the for statement accepts a list
> of values one would expect the usual rules about function calls and
> adjustment of returned values to apply.  If you can say

The usual rules do apply. Whenever a single value is expected and
multiple values are returned, the extra values are discarded.

> function f() return 2,10 end
> function g() return 4,12 end
> for a = f(), g() do print(a) end
2
3
4

As Dirk said, the comma-separated values are not a "list of values" in
the numeric "for" syntax. The fact that it uses "=" and "," just like
assignments is a coincidence, just like "(" is used both to
parenthesize sub-expressions and to start the list of arguments in a
function: two different uses of the same character. The "a = 1, 2"
inside "for a = 1, 2 do ... end" is not an assignment. The syntax
might as well have been "for a from 1 to 2 do ... end".

-- Hisham
http://hisham.hm/