lua-users home
lua-l archive

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



On Wed, Oct 5, 2016 at 3:14 AM, Mike Jones <mike@mikejones.in> wrote:
​...

include = require
function require(modulename)
    local before_ENV = table.clone(_ENV)
    include(modulename)
    prepend_functions(_ENV, before_ENV)
end

This lets me just call require("module1") and it will automagically
chain together any functions that get redefined.

Anyone see any flaws with this approach/with the code?

Thanks,
Mike

(I'm only starting to learn lua, but I am getting the impression that
"ugly hack" and "cool feature" are the same thing in lua? lol)


​I think it's a cool feature in Lua that the namespace (scope?) is just another table. So taking advantage of this to roll your own module system is par.

I'd say leave 'require' as is and use 'include' for the new module function. Or maybe even name it something longer and more descriptive e.g. 'include_prepend', as you don't have to type it often.​