lua-users home
lua-l archive

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


"Tom Miles" <Tom@creative-assembly.co.uk> wrote:

> I'm not going to ask why variables don't need to explicitly declared as
> I have learnt my lesson from my last post, but having spent yet another
> hour or so tracking down a bug due to a typo causing a variable to be
> automotically created and assigned a nil value,

Well, if it helps you... that was my first stumbling block as well and
it's still my #1 gripe about Lua.

I have since tried various avenues to have the compiler complain about
undeclared globals but it turned out that this is a knottier problem
than it appears. A fully-fledged compile-time implementation (as opposed
to runtime checks, see below) is not trivial. At the very least it would
significantly complicate the compiler implementation (which is currently
a model of clarity and efficiency). I decided that the benefits wouldn't
outweigh the effort.

> I was wondering if
> anyone had written a patch to stop this behaviour?

Not a patch as such but there's a file in the Lua distribution (see
etc/strict.lua) that somewhat alleviates the situation by doing runtime
checks. This works well and it's not as inefficient as it sounds. (FWIW,
I've done a module on the basis of this that supports modules as well,
see http://thomaslauer.com/comp/LuaStrict .)

-- 
cheers  thomasl

web : http://thomaslauer.com/start