[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Blog post on explicit locals in Lua
- From: Sam Roberts <vieuxtech@...>
- Date: Mon, 4 Jun 2012 12:08:44 -0700
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