lua-users home
lua-l archive

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


On Fri, 21 Oct 2011 11:49:47 -0700, Sam Roberts wrote:

The setting of the modules env to the module table doesn't just make
it "easy" to export functions, it makes it easy to not care whether
the functions is exported or not within the module;

module(....)

local function do_that() end

... lots of code that uses do_that()
----

When I decide I want do_that() exported, because its super useful, I
just remove the local, and its all good.

Contrast with the simplest of the many 5.2 module definition techniques:

local _M = {}

local function do_that() end
 ---[[ when this becomes
  function _M.do_that() end
all the code below will break, so then I guess we define a local
alias, for backwards compat?
 local do_that = _M.do_that -- LAME!
or we do a search and replace on the whole file, just because we now
export the function as part of the module?
]]

... lost of code that uses do that
return _M
------

Which is why I don't define modules like that. Do it at the end of the
file and the problem is gone:

    local function do_that() end
    ... lots of code that uses do_that()
    return {
      do_that = do_that
    }

--
Pierre Chapuis