[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: One more item to the collection of Lua hate-speech
- From: Wim Couwenberg <wim.couwenberg@...>
- Date: Sun, 4 Jul 2010 09:13:26 +0200
I also use a style to "localize" every global that is used at the top
of my script and never use seeall. Thinking about it, this is
probably my ordered list of motivations:
1) private references. Globals can be changed without affecting my code.
2) explicit scoping. I like that all identifier expressions can be
traced back to a definition either as a local or as an argument.
3) speed. Even though for a large number of cases this would be
irrelevant. I cannot help it I guess. :-)
This style does lead to "wrong" questions and suspicions (oh, are
globals not quick enough?)
Motivation 1) has the interesting possibility, also mentioned by Mark
Hamburg, to "switch off" the globals after all scripts are loaded and
executed. This could help to trap the very common bug that scripts
unintentionally reference/alter globals (e.g. typos). There can be
several approaches to this "switching off" but I didn't try any of
them out yet. (Could the new _ENV rule help here? I'm not sure.)
Globals could be deprecated if Lua would support some form of "import"
construction natively. For example "import os" would declare "os" to
refer to a package where it is in scope. Many programmers are already
used to such things (packages, namespaces, ...) so less to explain.
;-) And probably "constant" package references such as os.print could
compile into array indexing so it would be very fast. (The - growing
- set of such references into any package is known at compile time.)
At least to me something like this would feel as a natural next step
in the development of Lua.