lua-users home
lua-l archive

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


> > 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. In particular,
if the third is missing (the most common case), it will become nil.
The runtime then will need to change a nil increment to the default 1.

Now, suppose you write this code:

Inc = -1
for i = 10, 1, lnc do print(i) end

Currently this raises an error. With the new format, it will silently
skip the loop.

So, the real question is whether the increased flexibility is worth this
decreased check. Both sides seem reasonable to me.

-- Roberto