logoalt Hacker News

larme • today at 11:04 AM • 1 reply • view on HN

luajit and luau supporting 5.1 only is a feature: it freeze language so no more backward compatibility breaks

Lua has many quirks (global by default etc.) but with a language server like https://luals.github.io/, you can detect most of them easily plus optional type annotations.


Replies

strenholme • today at 1:37 PM

Also, it’s possible to force global variables to be explicitly declared.

  function declare(name)
    rawset(_G,name,false)
  end
  function exists(name)
    if rawget(_G,name) ~= nil then 
      return true 
    else return false end
  end
  setmetatable(_G, {
    __newindex = function(self,x) error("Undeclared global " .. x) end,
    __index = function(self,x) error("Undeclared global " .. x) end
  })
And now

  declare("foo")
  foo = 1
  -- Create name if it hasn’t 
  -- been declared yet
  if not exists("name") then 
    declare("name")
    name = 1
  end
More discussion: https://lua.org/pil/14.2.html