lua-users home
lua-l archive

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


> Why does Lua (5.0) allow the redefinition of locals within a single scope?

As others have pointed out, it does so because it's easier to implement
and more flexible to use. And that's Lua's philosophy...

But, as an exercise, I wrote a quick patch for doing what you want,
by adding this code to new_localvar (surprise!):

 % diff lparser.c,orig lparser.c
 149a150,153
 >   static int searchvar (FuncState *fs, TString *n);
 >   int k = searchvar(fs, name);
 >   int ok = ( k<0 || (fs->bl && k<fs->bl->nactvar));
 >   check_condition(ls, ok, "cannot redefine local");


I haven't tested it much, but it does work in the example below:

 local a
 local b
 do
	 local b
	 local c
	 do
		 local c
	 end
 end
 local c
 local b

The error message is not the best, because it does no include the name of
the variable and it gets the line wrong (actually the next token).

Also, I don't know why, but it does not find repeated function arguments,
that is, it's perfectly happy with this:
 function f(a,b,a) end

So, do not consider this an official patch!

Enjoy.
--lhf