lua-users home
lua-l archive

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


On Thu, Jul 15, 2010 at 10:21 PM, David Manura <dm.lua@math2.org> wrote:
>  local one = 1
>  local two = 2
>  local print = print
>
>  f(function(_ENV)
>    local counter = 1
>    global two
>    global function one() two() end
>    global function two() one() end
>    global function three()
>      countar = countr + 1
>      print(counterr)
>    end
>  end)
>
> [...] Finally, the global `print` needs to be accessed within the
> environment `_ENV`, which remains a problem because `print` doesn't
> belong inside `_ENV`.  That problem may be handled via the various
> usual techniques (`__index=_G` hackery, making `print` a local in the
> outer scope, or making `_G` a local in the outer scope and doing
> `_G.print` inside).

Actually, another option is to replace `local print = print` with
`global print`.  At least it's now the case that variables declared
with `global` resolve lexically across nested environments.  So,
you'll end up enumerating all top-level standard library variables
like this near the top of your program.  Given that it was decided to
avoid failing on gets to undeclared globals in order to avoid that
enumeration, if we'll be doing that anyway then there's no reason not
fail on gets to undeclared globals.