[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Better Syntax?
- From: Mike Jones <mike@...>
- Date: Tue, 4 Oct 2016 20:14:23 +0100
Thanks everyone for your suggestions, I think the fappend() approach
was what I was originally looking for but the inject method got me
thinking. After a little research I came up with this:
function table.clone(org)
local new = {}
for name, value in pairs(org) do
new[name] = value
end
return new
end
function prepend_functions(target, source)
for name, sourcefunc in pairs(source) do
if type(source[name]) == "function" and type(target[name]) ==
"function" and source[name] ~= target[name] then
local previousfunc = target[name]
target[name] = function (...)
local ret = sourcefunc(...)
if ret ~= 0 then return ret end
return previousfunc(...)
end
end
end
end
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)