4 ms·
Variables being global by default is definitely a wart. You can mitigate it a lot with a linter or with runtime tests (extending the global metatable). Local by
by ufo 12y ago
Variables being global by default is definitely a wart. You can mitigate it a lot with a linter or with runtime tests (extending the global metatable). Local by default would be a bad idea and worse than global by default, IMO, as it would mess up lexical scoping. The ideal would be having a compilation error by default.
- to3m 12y agoIt would be fine, I think. Just create the variable in the scope you want it, then assign to it somewhere else. If you get it wrong, you get it wrong, and there's scope for weird bugs from that - but the effect is at least localized, lexically. (Of course, the right thing to do is have explicit variable declarations...)
- ufo 12y agoLocalized bugs are still bugs. Better always require a variable declaration and have no defaults. Usually languages use local-by-default to avoid the need for variable declarations in the first place (in misguided manner, IMO) instead of doing so to avoid globals. This page might be interesting, btw: http://lua-users.org/wiki/LocalByDefault http://lua-users.org/wiki/LocalByDefault