lua-users home
lua-l archive

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


on 4/30/03 5:55 AM, Roberto Ierusalimschy at roberto@inf.puc-rio.br wrote:

> A simpler way is
> 
>     for k,v in pairs(t) do
>        local v = v
>        g_bindings[k] = function(x) return g(v,x) end
>     end

Yeah. I thought of that afterwards. I was thinking too much like a Scheme
user.

I like this because it is a way of explicitly saying "make a local copy of
the loop variable". If I don't need the local copy -- which is most of the
time -- then I don't pay the cost. The other choices seem much more
complicated in that one would need to apply a rule such as the following:

* If a loop variable is captured in a closure, then make a local copy within
the loop; otherwise don't.

The otherwise portion is there to avoid the extra work.

The rule would be reasonable since one generally shouldn't be changing the
value of the loop variables other than in the control statement and hence
capturing them in a closure with sharing is of little use (and could be
worked around if absolutely necessary by assigning to a local declared
outside the loop). The issue is whether one wants to pay the cost in the
compiler to detect this case.

Mark