[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: a new proposal for environments
- From: Duncan Cross <duncan.cross@...>
- Date: Thu, 25 Feb 2010 12:53:25 +0000
On Thu, Feb 25, 2010 at 12:21 PM, David Given <dg@cowlark.com> wrote:
> On 2010-02-25 00:53, Nevin Flanagan wrote:
> [...]
>>
>> Does this mean that you can supply any value to _ENV, as long as it
>> provides __index and __newindex metamethods as required?
>
> Hmm. If I'm *not* using globals, can I assign nil to _ENV to get runtime
> errors if I tyop the name of a local?
>
> local string_len = string.len
> _ENV = nil
> a = 5 -- oops, forgot to local this
>
> Is there any chance of a facility to produce *compile* time errors if my
> code tries to reference globals? It would have to be possible to turn it on
> halfway through a chunk, once I'd finished loading locals from the global
> table (as in the example above).
I agree that it'd be a good idea, but I don't think that it would be
practical for Lua to use the detection of _ENV being set to nil for
it. For one thing, it's possible (though not popular) for nil to have
__index and __newindex metamethods, so it's an unsafe assumption that
reading/writing to nil is always an error.
So I think if we did have this it would need to be a new kind of
block. Something like:
local do
-- global sets/gets here are compile time errors
end
...all right, so "local do" doesn't really make much sense, but you
get the idea.
-Duncan