lua-users home
lua-l archive

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


On Mon, May 24, 2010 at 7:33 PM, M Joonas Pihlaja
<jpihlaja@cc.helsinki.fi> wrote:
> Regarding speed of numeric for and setup overhead of your "for i in
> 100,1,1 do" style loop, you asked if the compiler couldn't optimise
> the latter into the equivalent of a numeric for.  The answer to that
> is probably "no, it can't (without significant effort)" because the
> user can change the metatable of numbers whenever they please to make
> the loop go up, down or sideways.

Only if you say so; the VM already ignores metamethods in several
situations that involve primitive values (change __add for numbers all
you want, addition is addition, for example). It would not be a
stretch for numbers and functions to have hardcoded behaviors in this
"for varlist in explist do" proposal.

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

Is that correct?

> Cheers,
>
> Joonas
>

--
Fabio Mascarenhas