gpt4 book ai didi

global-variables - 从 Julia 中的另一个模块更改全局变量的内容

转载 作者:行者123 更新时间:2023-12-04 18:30:08 25 4
gpt4 key购买 nike

我有两个模块,一个称为 mainLoop,另一个称为子例程:

module mainLoop
using subroutines
export memory
memory = zeros(Int,10)
foo(UInt32(17))
print(memory,"\n")

end

module subroutines
using mainLoop
export foo

function foo(x::UInt32)
mainLoop.memory[1]=x
end
end

我不知道如何从子程序模块中更改名为 memory 的数组的内容。我没有将数组作为参数传递给函数的选项,因此我必须使用全局变量。在 documentation , 它指出:

"Modules can introduce variables of other modules into their scope through the using or import statements or through qualified access using the dot-notation, i.e. each module is a so-called namespace. Note that variable bindings can only be changed within their global scope and not from an outside module."



通过变量绑定(bind),是否意味着您不能更改变量内容?

最佳答案

请注意,变量绑定(bind)只能在其全局范围内更改,而不能从外部模块更改。”

模块是你写的吗?因为这样您就可以轻松地,而不是“手动”更改绑定(bind),而是创建一个为您执行此操作的函数并将该函数与其余函数一起导出,即在您的模块视为其全局范围内

关于global-variables - 从 Julia 中的另一个模块更改全局变量的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com