使用彼此本地对象的模块 [英] Modules using each other's local objects

查看:59
本文介绍了使用彼此本地对象的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Vanilla Lua 5.2中,我有一个包含以下内容的模块:

In vanilla Lua 5.2, I have a module that contains:

  • 两个局部函数,A和B:B始终会调用A,A有时会调用B,有时会调用存储在C中的函数;

  • Two local functions, A and B: B will always call A, A will sometimes call B and sometimes will call the functions stored in C;

C:一个表(本地).它包含的表包含的表可以包含表...,最后将包含函数.这些函数可以调用A或B;

C: a table (local). It contains tables which contains tables which can contain tables... which will at the end contain functions. These functions may call either A or B;

然后是返回函数D,当使用require加载我的模块时,将返回D.它将呼叫A.

Then there's the return function, D, which will be returned when my module is loaded using require. It will call A.

最后,它看起来像这样:

At the end, it looks quite like this:

--don't pay attention to what the functions do:
--I am only writing them down to explain how they interact with each other

local A, B, C

C = {
    ...
    {
        function(a)
            B(a)
         end
    }
    ...
}

A = function(a)
    ...
    if (...) then
        B(a)
    end
    ...
    if (...) then
        C[...]...[...](a)
    end
    ...
end

B = function(a)
    A(a)
end

return function(s) -- we called this one D
    A(s)
end

现在,我的问题是这样的:C的声明使用其自己的局部变量,元表和所有其他内容,直到我将其声明括在do ... end块中为止.

Now, my problem is this: the declaration of C uses its own local variables, metatables and all that stuff, to the point I enclosed its declaration in a do ... end block.

对于所有花括号和缩进等,表中还有所有这些表以及换行符,它也相当长.所以我想将其放在自己的模块中,但随后它无法访问B.

It also is - with all those tables inside tables and newlines for each curly brace and indentation and so on - quite long. So I wanted to put it in its own module, but then it couldn't access B.

所以,我的问题是:有没有办法在加载时将B甚至A传递给声明C的文件?我的意思是,如果可能的话:

So, my question is: is there a way to pass B and maybe even A to the file where C is declared while loading it? I mean something like this, if it were possible:

--in the original module
local A, B, C

C = require("c", A, B)

...

然后在c.lua中:

local A, B = select(1, ...), select(2, ...)

C = {
    ...
    {
        function(a)
            B(a)
        end
    }
    ...
}

我真的不知道该怎么做.

I really have no idea on how to do it.

是否有一种方法可以将变量从需求文件传递到所需文件,而该文件不涉及在全局命名空间中插入的变量?

Is there a way to pass variables from the requiring file to the required file which doesn't involve the variables being inserted in the global namespace?

推荐答案

主模块:

local A, B, C

A = function(a)
    ...
    if (...) then
        B(a)
    end
    ...
    if (...) then
        C[...]...[...](a)
    end
    ...
end

B = function(a)
    A(a)
end

C = require("c")(A, B)

return function(s) -- we called this one D
    A(s)
end


c.lua:


c.lua:

local function C_constructor(A, B)
    local C = 
        {
            ...
            {
                function(a)
                    B(a)
                end
            }
            ...
        }
    return C
end

return C_constructor

这篇关于使用彼此本地对象的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆