lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Mark Hamburg writes:
> When used frequently, however, it can be a performance win since it
> eliminates a table lookup. I've started to use the convention:
>     local Corge_foo =

Alexander Gladysh writes:

> 1. I prefer to avoid using require.
> 2. There is no validation for to actually exist.

  local M = {}

  local Corge     = dofile 'qux/quux/Corge.lua'  -- or "import"
  local Corge_foo =
  local Corge_bar =
  local Corge_baz = Corge.baz
  -- local Corge_grault = Corge.grault  -- would raise

  local function alpha()
  M.alpha = alpha

  local function beta()
  M.beta = beta

  setmetatable(M, {
    __index =
      function(t,k) error("key not exist: " .. tostring(k)) end
  return M

If you prefer, the "key not exist" function could be defined
apart from the module:

  local Corge_foo = import(Corge, 'foo')    -- or
  local Corge_foo = checked(Corge).foo