lua-users home
lua-l archive

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


> A 'small hamming distance' is a nice way to say 'typo'.

Yup, but it sounds smarter in the first way.

> Lua programs are still very sensitive to spelling mistakes.  In
> theory, Lua could be patched so that _no global_ accesses are allowed,
> except for some way to get 'imports' in.  Then spelling mistakes will
> be genuine compile errors.

Not only in theory, I'm doing exactly that in my project after
initialization phase. It isnt yet a compile error but a runtime error,
but its better compared to some things becoming global by accident, or
something being nil just because of a typo. Dont ask me why it tests
for _ and __ I don't know, it was from the wiki.

----
-- Locks globals,
-- no more globals can be created
--
local function lockGlobals()
    local t = _G
    local mt = getmetatable(t) or {}
    mt.__index = function(t, k)
        if (k~="_" and string.sub(k, 1, 2) ~= "__") then
            error("Access of non-existing global '"..k.."'", 2)
        else
            rawget(t, k)
        end
    end
    mt.__newindex = function(t, k, v)
        if (k~="_" and string.sub(k, 1, 2) ~= "__") then
            error("Lsyncd does not allow GLOBALS to be created on the fly. " ..
                  "Declare '" ..k.."' local or declare global on load.", 2)
        else
            rawset(t, k, v)
        end
    end
    setmetatable(t, mt)
end

Kind regards,