Hacker News new | ask | show | jobs
by sshumaker 5258 days ago
You can fix that easily with a metatable.

  function nilguard(tbl)
    tbl = tbl or {}
    local mt = {
      __index = function(t,k)
        error("Invalid key: " .. tostring(tbl) .. "[" .. k .. "]")
      end
    }
    setmetatable(tbl, mt)
    return tbl
  end

  local myObj = nilguard({foo=10})
  myObj.crap -- should raise
For undefined global variables, just require("strict")