lua-users home
lua-l archive

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


Now I got what you were talking about (*facepalm*). Sorry for the noise. :-)

--
Fabio Mascarenhas


On Tue, May 25, 2010 at 3:20 PM, Fabio Mascarenhas <mascarenhas@acm.org> wrote:
> On Tue, May 25, 2010 at 2:58 PM, Roberto Ierusalimschy
> <roberto@inf.puc-rio.br> wrote:
>>> 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).
>>
>> I cannot see how to avoid a type check at each iteration, as each
>> iteration needs completely different behavior in each case.
>
> Sorry if the description was not clear, what I meant is that a statement like:
>
> for var_1, ···, var_n in explist do
>  <block>
> end
>
> would be equivalent to the code:
>
> do
>  local e1, e2, e3 = explist
>  if type(e1) == "number" then -- numeric for
>    local var, limit, step = e1, tonumber(e2), tonumber(e3) or 1
>    if not limit then error() end
>    while (step > 0 and var <= limit) or (step <= 0 and var >= limit) do
>       local v = var
>       <block>
>       var = var + step
>    end
>  else -- __iter or regular generic for
>    local f, s, var = e1, e2, e3
>    if type(f) ~= "function" then -- __iter metamethod
>      f, s, var = metatable(f).__iter(f)
>    end
>    while true do -- generic for
>      local var_1, ···, var_n = f(s, var)
>      var = var_1
>      if var == nil then break end
>        <block>
>      end
>    end
>  end
> end
>
> The values of the explist when you enter the loop decide what "kind"
> of loop the for is, you can't switch from generic to numeric in the
> middle of the loop.
>
>> -- Roberto
>>
>
> --
> Fabio Mascarenhas
>