lua-users home
lua-l archive

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


On 21 July 2018 at 06:06, Gavin Wraith <gavin@wra1th.plus.com> wrote:
> local D = { }
> global = setmetatable ({ }, {
>     __newindex = function (t, n, v) rawset (_G, n, v) end,
>     __index = rawget,

This assignment is redundant.

>     })
> global ._PROMPT = "> "
> setmetatable (_G, {
>   __newindex = function (t, n, v)
>   if not D[n] then
>     local w = debug.getinfo (2, "S").what
>     if w ~= "C" then

Why do you exclude C functions from the limitation?

>       error ("\nattempt to write to undeclared variable " .. n, 2)
>     end -- if
>     D[n] = true
>   end -- if
>   rawset (t, n, v)

You may want to make this a tail call for optimization purposes.

>  end ,
>  __index = function (_, n)
>       if not D[n] then
>        error ("\nattempt to read undeclared variable " .. n, 2)
>       end -- if
>   end,
> } )
>
> So the first assignment to a global variable x
> has to be
>
>    global.x = foo
>
> but before that any reference to x raises an error.