lua-users home
lua-l archive

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

On Aug 5, 2005, at 18:23:18, Chris Marrin wrote:
and everything falls apart. The global 'a' got changed as a side effect of the call to baz() and the value returned from foo is wrong. This is very difficult to track down because I am a naive script author and I actually don't even know that there is such a thing as local! And even if I do, the fact that everything works without error until I make this small change makes it very hard to debug.


 this is exactly the case that has me worried.

It seems like the solution is to not have any default at all. You would have a 'global' keyword to declare a global variable. That way, the above example would give errors on the first assignment to 'a'. I would see the mistake and add a 'local' keyword and all would be well.

Even without getting rid of the global-by-default rule, a global keyword would help with making the code more clear. It would enable the writing of a lint program for Lua which could warn of undeclared globals. You could add a global keyword and get rid of the warning. In the runtime, this keyword would be a nop. This lint facility could even be easily added to the lua compiler as a command line flag.

I suppose that once there was a "global" keyword in the language, it should be fairly easy to make the compiler error or warn on undeclared variables. Even if I'd probably want a command-line option on the lua interpreter to turn on this warning explicitly so old code still runs without changes.

The point to me simply is that most people will in general *want* locals more often than globals, and thus I can understand why other languages chose this "safer" scheme as the default.

-- M. Uli Kusterer