[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: global-by-default (was: One more item to the collection of Lua hate-speech)
- From: Geoff Leyland <geoff_leyland@...>
- Date: Thu, 8 Jul 2010 10:11:45 +1200
On 8/07/2010, at 9:22 AM, Roberto Ierusalimschy wrote:
>> On 8/07/2010, at 6:00 AM, Roberto Ierusalimschy wrote:
>>
>>> One option would be to ban assignment to globals at all inside
>>> functions. Another option would be to only allow assignments to
>>> global variable previously seen in that chunk.
>>
>> Could this be achieved this with a variant of strict.lua?
>
> Sure, but only to a point. strict.lua is a dynamic check, not a static
> one. It still needs a minimum of tests to find errors.
Good point!
> It does not provide an easy way to circumvent the restriction.
Surely you can build an easy way to circumvent the restriction?
Something like (5.1):
global = setmetatable({}, { __newindex = function(t, i, v) rawset(getfenv(2), i, v) end })
global.A = 1
> And it is optional.
Isn't that a good thing? I hope it's not impolite to quote you to yourself, but: "A fundamental concept in the design of Lua is to provide meta-mechanisms for implementing features, instead of providing a host of features directly in the language".
If you're thinking of going to the point of banning globals under certain circumstances unless people use _ENV.myglobal, why not consider adding a "global", "newglobal" or "setglobal" keyword? (The more I think about it the less sure I am that I understand whether any of those options could work).
Geoff