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 = Corge.foo

Alexander Gladysh writes:

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

  local M = {}

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

  local function alpha()
    Corge_foo()
    Corge_bar()
  end
  M.alpha = alpha

  local function beta()
    Corge_bar()
    Corge_baz()
  end
  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