Hacker News new | ask | show | jobs
by weberc2 3313 days ago
I forgot to emphasize that I'm particular about syntax. I amended my comment accordingly. I also don't like the global-by-default bit, but maybe my ideal language could be a syntax layer atop Lua?
2 comments

Metatables are your friend.

    -- LuaJIT
    
    local oldG = _G
    local newG = setmetatable({}, {
        __index = oldG,
    })
    setmetatable(oldG, {
        __index = function(t, k)
            return rawget(newG, k)
        end,
        __newindex = function()
            error('attempt to assign global variable')
        end,
    })
    _G = newG
If you want to assign a global variable, `x = 5` will error but `_G.x = 5` won't.
If you ignore the class support of moonscript, you can pretty much use it to do what you want. It doesn't have the "global by default" silliness, to start with.