lua-users home
lua-l archive

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


> On Nov 30, 2014, at 4:06 PM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> 
>> what if environments had stacks?
> 
> We already have that. No need for a keyword.
> 
> 	x=1
> 	print(x)
> 	do
> 		local _ENV={print=print}
> 		x=2
> 		print(x)
> 		do
> 			local _ENV={print=print}
> 			x=3
> 			print(x)
> 		end
> 		print(x)
> 	end
> 	print(x)
> 
> The _ENV mechanism was created exactly to allow this kind of thing
> without resorting to magic.
> 

Think the OP was thinking of a “union” model where lookups searched down the stack for the first match. But even in this case there is no need for any new keywords, as it’s easy to create the stack using a metatable on the _ENV table.

—Tim