【问题标题】:Lua specifying libraryLua 指定库
【发布时间】:2013-10-29 17:46:28
【问题描述】:

在我的 Lua 脚本中,我有多个使用相同“结构”的库。 例如,我有一个包含 'require('b')' 和 require('c') 的.lua。 b.lua 和 c.lua 都有 info 函数。 b.lua 让它打印“b”,c.lua 让它打印“c”。现在我希望能够指定我在 a.lua 中使用的内容。

【问题讨论】:

    标签: lua require


    【解决方案1】:

    通过使用表将您的“b”和“c”模块放入不同的命名空间,然后从a.lua 明确限定要使用哪一个。例如:

    -- b.lua
    local function info()
      print "b"
    end
    
    return { info = info }
    

    -- c.lua
    -- another style
    local M = {}
    function M.info()
      print "c"
    end
    
    return M
    

    -- a.lua
    b = require 'b'
    c = require 'c'
    
    b.info()  -- prints "b"
    c.info()  -- prints "c"
    
    local info = b.info -- ok you really want 'b'
    info()    -- prints "b"
    

    【讨论】:

    • 谢谢,但现在我在使用协程时遇到了错误。你也有解决方案吗?
    • @scheurneus 您应该将其作为一个新问题提出。
    猜你喜欢
    • 2013-10-31
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2022-07-28
    相关资源
    最近更新 更多