lua-users home
lua-l archive

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


This is covered thoroughly here:

http://lua-users.org/wiki/LocalByDefault

This repeating question seems to presuppose that the way some "other
language" does it is free of problems. All choices have problems,
including allowing variables to be mutated at all (scala and erlang
fans, speak up!). Python and ruby have struggled with their choices,
too.

Ruby attempts to determine whether you wanted a new local created or
not by searching lexical scopes for a var you could be assigning to.
The price is that there needs to be a different syntax (the $) for
global variable access, and that changing a name of a var in an outer
scope can change the meaning of an inner scope's assignment. This
latter problem was dealt with in 1.9 by adding new syntax to blocks to
make sure that block local vars stay block local.

python forces extra syntax for accessing globals (the global keyword),
and even worse, doesn't allow you to assign to variables that are in
your scope. This leads to some very ugly workarounds in code written
using a functional style (creating locals that need to be assignable
by inner scopes as collections, and you assign into the collection,
ugh!), and in later versions, to the nonlocal keyword.

Lua's rules in contrast are simple and uniform (locals are explicitly
created - "explicit is better than implicit", right? :-) - if no
locals exist in the lexical scope, they fallback to global). If you
don't like that, you can change the behaviour. "mechanism, not
policy".

Cheers,
Sam