[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: upvals in Lua bytecode
- From: Florian Weimer <fw@...>
- Date: Thu, 02 Jun 2011 11:24:32 +0200
* Reuben Thomas:
> On 2 June 2011 08:39, Geoff Leyland <email@example.com> wrote:
>> Is "outer" new?
> PEP 3104, 12th October 2006.
It's actually "nonlocal".
>> I almost immediately got scope paranoia using Python, but that was a
>> long time ago.
> Since Python is local by default,
It's more complicated than that.
Assignment to a variable anywhere in a function makes it
function-scoped, for the entire function. Without such an assignment,
variable references are global.
In contrast, in most languages, local variables are block-scoped. The
Python approach does not make much sense for them. Let's assume you
want to initialize x based on some condition. In Python, you can
x = f1()
x = f2()
If x is not of function scope, then this has not the intended effect.
You'd actually have to write this instead:
x = (function()
if cond then
Anyway, to me, the local-vs-global discussion is tied to the module
system and some sort of phase distinction in the compiler. If there
was a way to plug into global name lookup in the compiler, we could
accurately describe to the compiler what global variables exist, and
the compiler could error out for unknown global variables. It might
even be possible to compile expressions such as string.byte to a
constant reference in the byte code, so that the need for the
"local byte = string.byte" pattern goes away.
But in the end, it's more of a tools issue. Microsoft seems to have a
the code in a sandbox and collect symbol information from the result.
Thus, they can warn about references to non-existing variables in the
editor in the editor. There are several projects to implement similar
functionality for Lua.