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 

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

...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" 

	procedure do_stuff(y)
		for local x = 1, 10
			for local y = 1, 10

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

.------- - Games, SDL examples  -------.
| - 2.5D rendering engine       |
| - Music/audio engine          |
| - Real time scripting         |
'-- - Rheology instrumentation  --'