Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


It 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...)


Localized 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: