【发布时间】:2015-11-18 06:06:33
【问题描述】:
我有两个由 lua 编写的不同库,但函数名相同。
如何在不修改库源的情况下选择其中之一?
require ('A') --A lib
require ('B') --B lib
test() -- function from B
如果我想要 A 的测试功能,我该怎么做?
这是我尝试过的一些方法。
-
LuaJ loading two functions with the same name from two different LuaScripts
我找到了这个,但我不想修改 lib 源代码。
-
将函数另存为不同的名称,如下所示:
require ('A') --A lib _G.testA = test require ('B') --B lib _G.testB = test testA() -- function from A testB() -- function from B但如果有一天一个新的函数名称 testA() 或 testB() 会再次中断。
而且我仍然需要将所有存在的 test() 更改为 testA() 或 testB()。
有没有更好的办法?
【问题讨论】:
-
“修改lib源代码”是“更好的方法”。
-
理想情况下,您使用的库不会污染全局命名空间,但如果它们污染了,您可以编写一些对环境进行差异化并将库的全局状态移动到范围内的东西。
-
@Mud 你能给我看一些示例代码吗?
标签: lua