lua-users home
lua-l archive

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


On Fri, Oct 9, 2020 at 3:41 AM 张伟智 <robotkzhang@gmail.com> wrote:
> why pairs doesn't return a C closure with an integer upvalue to hold
> the last key's position in Node[] ?

Is there a useful notion of a "C closure" that could be used for this?
It needs to free the associated integer when it is GCed.

The issue you point out is one reason that the iterator protocol could
be improved by separating the first user-visible value in the generic
for loop from the control variable.

That is, instead of being equivalent to this code:

  do
    local _f, _s, _var = explist
    while true do
      local var_1, ... , var_n = _f(_s, _var)
      _var = var_1
      if _var == nil then break end
      block
    end
  end

A loop declaration like

  for var_1, ..., var_n in explist do block end

should perhaps be equivalent to this code:

  do
    local _f, _s, _c = explist
    while true do
      local _c, var_1, ... , var_n = _f(_s, _var)
      if _c == nil then break end
      block
    end
  end