lua-users home
lua-l archive

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


On Tuesday 03 October 2006 18:14, Javier Guerra wrote:
> On Tuesday 03 October 2006 9:49 am, David Olofson wrote:
> > I wasn't really aware of this issue at first (haven't designed all
> > that many languages before), and of course, got it wrong in EEL
> > (local by default), did some scripting, realized it was just plain
> > stupid, switched to explicit declarations for everything, and
> > never looked back.
> 
> i've read (and agree with) the long and theoretical explanations
> about why local by default is the wrong choice; but could you share
> any short anecdote that convinced you of this?

Unfortunately, it doesn't seem like I've kept any real life examples 
around.

Anyway, Rici's second example demonstrates one of the issues. (In EEL, 
I require that upvalues are explicitly declared as such.)

Here's a fictive example of another problem, which might actually have 
played a bigger part in making me aware of the problem (EEL code, 
"local by default" version):

	procedure do_stuff(y)
	{
		for x = 1, 10
			for y = 1, 10
				<stuff>;
	}

...which would compile and run just fine, completely hiding the fact 
that the two loops are really rather different, and the inner one 
most probably isn't doing what's intended. And what if x was declared 
outside do_stuff()...? You can't even tell without studying all code 
above this procedure in the file.

Here's the same code, written as intended, using the new "no defaults" 
version:

	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.


//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  --'