Hacker News new | ask | show | jobs
by timmaxw 5258 days ago
I've used Lua for a couple of personal projects. My main objection to it is that accessing an undefined variable or member returns nil rather than throwing an exception. If you make a typo, you don't find out until you try to call or perform arithmetic on your nil value. Since inserting nil into a table just deletes that key of the table, the use site might be several steps away from the typo.

Other than that, I agree with the other posters here. It's an impressively lightweight and elegant language. It's especially good for embedding: its C integration is next to none, it's easy to sandbox if you want to run untrusted code, and the interpreter doesn't use any global variables.

2 comments

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")
You can set __index on the global table's metatable. Basically: "run this hook whenever I try to access an undefined field".

    setmetatable(_G, {__index=function(global_env, name) print("unknown: " .. name) end})