lua-users home
lua-l archive

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


On Tue, May 25, 2010 at 1:12 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> I will try to summarize what I think is being proposed here:
>>
>> -- first value of explist is a number
>> for var in num, [num, [num]] do
>>   -- same as current for var = num, [num, [num]] do
>> end
>>
>> -- first value of explist is a function
>> for var, ... in func, ... do
>>   -- same as the current generic for
>> end
>>
>> -- other cases
>> for var, ... in val, ... do
>>   -- do generic for using the result of
>>   -- getmetamethod(val, "__iter")(...) as the explist
>> end
>
> Maybe I am missing something here, but way before the problem of
> changing metamethods there is the problem of finding, at compile time,
> whether a for loop is numerical, unless it uses literal numbers.
> Syntactically, the three previous cases are all equal.

Yes, that is a problem with this proposal, it adds the overhead of an
extra type check to every numeric for (at least it is a one-time cost,
not per-iteration). One possible optimization would be to fall back on
the existing numeric for opcode in the cases where the first
expression in the explist is a constant number, which I think would
cover most of the current uses of the numeric for.

The question would be, is it desirable to have both kinds of for loop
have the same syntax? I am undecided on that, but have never been a
big user of the numeric for outside of benchmarks and loops where I am
using the control variable to compute stuff instead of indexing. I
still prefer using ipairs to iterate over sequences, even though I
know it is not as efficient.

> -- Roberto
>

--
Fabio Mascarenhas