lua-users home
lua-l archive

Re: Is there anyway to make a new Lua closure sharing the same code with old one but with different env table?

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


On 21 June 2010 08:48, Kristofer Karlsson <kristofer.karlsson@gmail.com> wrote:
> function functionfactory()
>    local upvalue1, upvalue2
>    return function()
>        return upvalue1 + upvalue2
>    end
> end
>
> local clone1 = setfenv(functionfactory(), env1)
> local clone2 = setfenv(functionfactory(), env2)
>
> clone1 and clone2 now shares prototype and upvalues, but have different
> environments
Actually, each of the returned functions have their own upvalues, they
aren't shared. If you lifted "local upvalue1, upvalue2" out of
functionfactory, then they would be shared.
 henk

AltStyle によって変換されたページ (->オリジナル) /