lua-users home
lua-l archive

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


Steve brought up Lua gotchas in a different context. I didn't have
"strict" loaded and had a long bout with a hidden bug, leading me wish
that strict global creation was a more integral part of Lua.

This has been a topic from the past. I bring it up now because of 5.3,
although I'd much prefer a release of 5.3 to be much sooner, as I
think it's a nicely sized release, as is.

##Motivation and Acknowledgement of Existing Alternatives:

`strict` is available and works well. Also, it doesn't slow much of
anything down because it's only activated when a global is explicitly
set to nil or when one is accessed without being set.

Because `strict` is not part of the standard library, it is not as
conveniently at hand. Therefore, people reinvent it or don't include
it and, as a result, it's not part of the language's "culture." That
is, it can't become a "best practice" to enable it or include it and
it can't be considered properly in modules that people may write. It's
not a bug if they create globals, necessarily, and you can't expect
someone else to `rawset(_ENV, x, 1)` when the do.

Second, using strict makes assigning globals somewhat awkward in that
an assignment is now a function call. This might be good or bad; good
if one considers globals to be something that should be made
difficult.

I think it's bad, because it might be motivation to not use strict or
to consider it a "hack."

Lua is very flexible and just about any language that isn't already
present can be created at run time, including this. I think a keyword
is warranted, because it is more likely to help people that would
otherwise not load it and it creates a standard way to do something
that would benefit from being standard.

##Proposal

My idea (probably not a new one) is in two parts:

1: `global` keyword is (probably) shorthand for rawset(_ENV, var, val)

2: _STRICT is a value that is global and defaults to `false` (or nil,
but one must be able to assign it globally).

Example
```lua
print(_STRICT)
--> false
x = 1
print(x)
--> 1
_STRICT = true
global y = 2
print(y)
--> 2
z = 3
--> error: Implicit assignment of global "z" attempted at line 3.
```

This way, "Lua as a config file" is not effected and there is now a
way to make globals explicit and to do it in a way that looks like
part of the language, while doing it.

I must acknowledge that this idea might not rise to the level of need,
or it may also not fit well within a microwave. :) I do think it would
improve Lua and the way that people use it.

Thanks!

-Andrew