lua-users home
lua-l archive

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

> Whether or not the function is anonymous (by that I mean, whether or
> not it can be saved to some global value by the function you pass it
> to as an argument) seems irrelevant to whether a new closure should be
> made. The function's operation is orthogonal to the environment, where
> the closure was made, etc. (all because there are no upvalues). Even
> if the function relied on the global environment, I don't see a reason
> for a new closure. If it's too difficult/costly to implement that's
> one thing, but I don't see a theoretical reason for creating a new
> closure over a function with no upvalues.

a = {}

function f (c)
  a[#a+1] = c
  setfenv(c, {})

for i=1,10 do f(function () end) end

assert(getfenv(a[1]) ~= getfenv(a[2]))

(I am not claiming this is useful for any particular task; I am only
showing that not creating a new closure is not an optimization: it
changes the semantics of the language.)

-- Roberto