|
On 12/11/2012 17:22, Luiz Henrique de Figueiredo wrote:
Actually, this results in an error, unless mod defines mod.print... Anyway, if you remove the outside declaration of a, then the code works as intended: local print=print do local _ENV = require 'math' print(pi) end
Maybe the point of Egor was that a pi in the required environment does not impose itself. Moreover, it may not be trivial to guess that a local (if the term is still right...) from the surrounding module's top level may override what we intend to import.
The interplay of local (which when relevant to the present topic are often at module top level, that is more or less what other languages call 'globals') and globals in the Lua sense, with environments which are or not _G, is not easy to think for me. Actually, I don't understand anymore what 'local' means (in different cases, if that changes), or the point of (Lua) locals at all when we can freely define environment for every piece of code, or any size. For me, until now, local meant "keep this in the present scope (env)". But with Lua environments, it's as if _globals_ are confined in envs, while --as the example above illustrates with both pi and print-- locals seem to freely transgress their borders.
I'm lost. Denis