[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Is there anyway to make a new Lua closure sharing the same code with old one but with different env table?
- From: Henk Boom <henk@...>
- Date: Mon, 21 Jun 2010 16:35:49 -0400
On 21 June 2010 08:48, Kristofer Karlsson <firstname.lastname@example.org> wrote:
> function functionfactory()
> local upvalue1, upvalue2
> return function()
> return upvalue1 + upvalue2
> local clone1 = setfenv(functionfactory(), env1)
> local clone2 = setfenv(functionfactory(), env2)
> clone1 and clone2 now shares prototype and upvalues, but have different
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.