lua-users home
lua-l archive

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


On Sun, Jan 23, 2011 at 19:13, Henk Boom <henk@henk.ca> wrote:
> 2011/1/22 Pierre-Yves Gérardy <pygy79@gmail.com>:
> I do think that with the new _ENV semantics there's more flexibility
> to protect against the accidental use of globals, and that this is
> worth exploring.
>
> However, I don't feel that declaring variables on use instead of on
> creation is win. It ubfuscates scoping since it's harder to see
> declarations. It also doesn't really make much sense for lua, since
> upvalues are only introduced at function scopes while locals exist in
> much more finely grained scopes. That is, upvalues and scope are
> entirely different issues.

I don't understand why you make such a difference. Upvalues are locals
declared in the surrounding lexical scopes.

> As an experiment, I took the update() function from some code that I had here:
>
> https://github.com/henkboom/pax-britannica/blob/master/scripts/fighter_ai.lua
>
> I manually declared all of the variables from higher scopes as
> upvalues, then removed all of the local declarations. The result is a
> bit more verbose, and more importantly I have a lot of trouble
> figuring out what the scopes of variables are (search for 'upval' to
> see how many are needed):

It is indeed less readable. I hadn't thought about the scopes
introduced in loops and conditionals, for example.
I've tried other options (putting several upvals in a row, the old
%prefix, ...) but they all look worse than the current standard.

The only readable option is to declare all upvalues at the top of the
function, and then make the loops, conditional and do blocks look for
them up to the enclosing function level. However, an explicit way to
declare locals is still useful in a lot of cases.

One could perhaps make the local keyword optional (and drop my upval
proposal). When the parser finds a naked assignment, it would look at
locals, then upvalues, and if none are found, declare a local in the
current scope and assign to it.

Overall, you're rignt, it doesn't add much to the language. Sorry for the noise.

--Pierre-Yves