lua-users home
lua-l archive

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


> Very true, but C requires everything to be declared up-front.  Lua
> will automatically declare an unknown variable to be global
> (global-as-default), so any misspelling will cause a runtime problem
> since the new variable will have the value 'nil'.  (Perhaps it
> wouldn't be so bad if the initial value was something like
> 'undefined').
> 
> It's a question of scale.  A 100-line script does not have to worry
> too much about locals and globals, but a 100Kloc program has to be
> more careful, especially with global-as-default.

With "local-as-default", a misspelling in the *use* of a variable
will also give you a nil value, which I think is close enough to
'undefined'. In my view (and in my experience), this is not a big
problem. Undefined variables are easily detected in basic tests.
Moreover, as others already pointed out, misspelling may also happen
when accessing table fields (e.g., math.coz), with the same consequences
of misspelling in the use of globals; so, solving misspelling problems
only for globals will not improve much the situation, and solving
misspelling problems for table accesses is completely out of Lua scope.

The problem that concerns me is assignment to globals meant to be
locals, either because someone only forgot to add 'local' or because he
knows no better. This kind of bug is easy to write and hard to find.

-- Roberto