lua-users home
lua-l archive

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


Edgar Toernig wrote:

> - Now to the for-loops.  I guess that most people - when hearing that
> 4.0 has for-loops - thought about looping over tables and I was very
> disappointed to realize that you only implemented numeric style loops.
> Yes, numeric loops are useful but don't you think that iterating over
> tables is even more wanted?  If you did not have enough time: just
> ask for it and I will write an implementation!  IMHO, there should be
> no 4.0-final without _real_ for-loops.
>
> - Another thing with for-loops.  Why did you to made the iteration
> variable an automatic local?  I considered this while implementing
> for-loops for 3.2 but discarded that idea pretty fast.  It's too ...
> inconsistent!?! ... special? ... ugly!  Look at this:
>
> n="foo" -- global n
> print(n) -- foo
> for n=1,3 do print(n) end -- 1 2 3
> print(n) -- foo
>
> Ok, sometimes it may be easier to not have to write the "local n" and
> the generated code saves one instruction but it looks so ... wrong.

I always write C++ loops as "for (int i; ... )", so the Lua for loop is
perfectly natural to me.  In practice I rarely need to access the iterator
variable outside the loop, and maybe that's when I'm writing bad code.  It
keeps a for loop "local", allowing you to cut and paste it around without a
thought.

On the other hand if Python-like for loop iteration over tables were
available as you suggested, then I rarely need numeric iterators.  (But
really this is already available with foreach/i... just need to improve the
upvalue situation for function objects.)

Keep up the insightful posts!

-John