lua-users home
lua-l archive

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


On Wednesday 04 October 2006 10:09, Nick Gammon wrote:
> 
> On 04/10/2006, at 6:01 PM, David Olofson wrote:
> 
> >
> > 	procedure do_stuff(y)
> > 	{
> > 		for local x = 1, 10
> > 			for local y = 1, 10
> > 				<stuff>;
> > 	}
> >
> > This will not compile, as the declaration 'local y' in the inner
> > loop tries to redeclare the 'y' argument of the procedure. Same
> > thing if x was declared outside the procedure; it wouldn't compile
> > because 'local x' would be an attempted redeclaration.
> 
> 
> However Lua currently allows redefinition, eg.
> 
> local y = 1;
> f ()  -- uses 1st y
> local y = 2;
> f () -- uses 2nd y
> 
> I'm not sure the the solution doesn't introduce its own problems,  
> possibly numerically as many as the ones it tries to fix.

I decided to require a keyword 'shadow' for redeclarations in EEL. So, 
you can still do it, but you need to be explicit.

Still not sure if even that feature has a valid reason to exist... 
Feel like a mistake, as IIRC, I implemented the 'shadow' feature 
"just because" - not because I actually needed it.

Do you ever actually *need* to do this? Why not just use a different 
name, if you really need a separate variable in the same scope?


> How about this? Make a "lint" program that detects these things,  
> outside the Lua interpreter itself, which is trying to be small and  
> compact.

Another option is compiler warnings. Should probably be optional, and 
maybe subdivided in some classes, so people can decide how paranoid 
they want the compiler to be. (Sort of like building the lint tool 
into the compiler - but the compiler generally has a lot of useful 
information that isn't available without essentially reimplementing 
half the compiler in the lint tool.)

The EEL compiler throws warnings for some things that are risky, but 
not clearly wrong. My general rule of thumb is, "If it's dangerous 
and not really useful, make it an error."

Problem is, this solution doesn't work very well for me, as I have a 
habit of making sure my code doesn't generate warnings even with all 
warnings enabled - so as far as I'm concerned, I might as well turn 
those warnings into errors...! :-D


//David Olofson - Programmer, Composer, Open Source Advocate

.-------  http://olofson.net - Games, SDL examples  -------.
|        http://zeespace.net - 2.5D rendering engine       |
|       http://audiality.org - Music/audio engine          |
|     http://eel.olofson.net - Real time scripting         |
'--  http://www.reologica.se - Rheology instrumentation  --'