lua-users home
lua-l archive

local-by-default revisited

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


What follows is another "local by default" proposal. IMO it's simpler,
less obtrusive and yet gives better results than other proposals. It's
intended for Lua 4.1.
The changes are:
1. Add a new keyword "outer" which works like "local", except that it
 declares the given name to refer to the innermost variable of the
 same name in an enclosing function or, if there is none, to a
 global of the same name (even if none currently exists).
2. The first assignment to a variable in a function implicitly acts as
 a "local" declaration unless it has already been declared in that
 function, explicitly or implicitly. Assignment outside a function
 works as at present, as does assignment to table elements.
Note:
a. The ability to do configuration file style global assignment is
 retained, at the cost of making assignment inside functions work
 differently from outside. In practice this shouldn't be a problem,
 as few Lua scripts contain complex code at the top level. If you
 need local variables in top-level code, you must simply declare
 them as at present.
b. The only change to the syntax is the addition of "outer".
c. The only semantic change is to the meaning of assignment within
 functions.
d. Assignments only make the variable local where they occur, rather
 than for the whole function. This agrees with the principle of
 least surprise: you don't have to read an entire function to find
 out whether a given variable is local (and nor does the compiler,
 so the implementation is simple).
[I've put this on the Wiki too as LocalByDefault under
FeaetureProposals.]
-- 
http://sc3d.org/rrt/ | competent, a. underpromoted

AltStyle によって変換されたページ (->オリジナル) /