Hacker News new | past | comments | ask | show | jobs | submit login

Ehh, local being a keyword (instead of local default, global only if required) was a significantly bigger gaff. The other would be no nulls in tables. 0/1 based indexing is familiarity only.



Requiring a keyword to declare a variable is a good thing. Otherwise, it'd be easily to accidentally declare new variables when you meant to assign to an existing one. Since global is the default, starting a program with something like this effectively keeps that from happening:

  local gindex = {}
  setmetatable(_G, {__index = function(t,k)
    local v = gindex[k]
    if v == nil then
      error("Bad read of global variable " .. tostring(k), 2)
    else
      return v
    end
  end, __newindex = function(t,k,v)
    error("Bad write of global variable " .. tostring(k), 2)
  end})
  for k,v in pairs(_G) do
    gindex[k] = v
    rawset(_G, k, nil)
  end




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: