lua-users home
lua-l archive

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


On Fri, Sep 14, 2012 at 7:20 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> > Why this simple code
> >
> > for i = 1, 5 do print(i) end
> >
> > cannot be written in more general form ?
>
> The only reasons I can think of are implementation optimization, and "a
> heritage" - everyone is used to this kind of for syntax.

Other reasons include error detection. With the current syntax, Lua
can check whether the 'for' actually has two or three values, and
add the default 1 increment when it has only two. In the generic
form, Lua will have to adjust the list to three results.

Why would it have to do so?  There is a difference, after all, between an _expression_ resulting in the two values <1, 10> and an _expression_ resulting in the three values <1, 10, nil>.

Couldn't the runtime check the number of values resulting from the _expression_ before adjusting?  In principle, it seems you could enforce "step must be a number" only when the _expression_ resulted in 3 or more values, before adjustments.  This would give the generality being asked for here, without losing the error checking.

Greg F