struct - 包含一个模块并从 Julia 的另一个模块中调用它的一个函数
问题描述
假设我有一个模块,Module1 定义了一个结构和一个 fit() 函数。我有另一个模块,称为 Parent,它包括 Module1 并定义了一个 test() 函数,该函数也调用 fit()。如果我从模块 1 定义结构,然后从 Parent 调用 test(),我会遇到命名空间问题。这是此示例的代码:
#this module would be in a separate file
module Module1
struct StructMod1
end
export StructMod1
function fit(s::StructMod1)
end
export fit
end
module Parent
#including the module with include("Module1.jl")
module Module1
struct StructMod1
end
export StructMod1
function fit(s::StructMod1)
end
export fit
end
#including the exports from the module
using .Module1
function test(s::StructMod1)
fit(s)
return s
end
export test
end
using .Parent, .Module1
s = Parent.Module1.StructMod1()
@show test(s)
s2 = StructMod1()
@show test(s2)
和输出
test(s) = Main.Parent.Module1.StructMod1()
ERROR: LoadError: MethodError: no method matching test(::StructMod1)
Closest candidates are:
test(::Main.Parent.Module1.StructMod1)
或者,如果我将 using .Module1 替换为 using ..Module1 ,则 s2 的定义有效。但是,当使用 .Parent 调用时,我必须确保 Module1 已经加载。
用一个模块定义结构然后将其与另一个模块的功能一起使用的最佳方法是什么?
解决方案
双重包含导致错误。在您的示例中,Parent.Module1
与Module1
. include
将其视为愚蠢的复制/粘贴很有用。即使它们共享相同的源文件,双重包含也会使它们显示为两个不同的模块(如您的示例所示)。
解决方案是include
只使用一次并使用相对导入来引用模块。
至于组织整套模块,我喜欢include
在主模块中按顺序排列所有模块,然后在需要的地方使用相对导入。我发现这是最简单的方法。
最终版本看起来像这样:
# Module1.jl
module Module1
struct StructMod1
end
export StructMod1
function fit(s::StructMod1)
end
export fit
end
# Parent.jl
module Parent
using ..Module1
function test(s::StructMod1)
fit(s)
return s
end
export test
end
#Main.jl
include("Module1.jl")
include("Parent.jl")
using .Parent, .Module1
s = Parent.Module1.StructMod1()
@show test(s)
s2 = StructMod1()
@show test(s2)
推荐阅读
- javascript - 在 javascript 类声明中,在构造函数和主体中声明方法有什么区别?
- javascript - 将 Vue2 CLI 转换为 Vite
- reactjs - react.js 事件处理程序未返回更新的数据
- multithreading - Rust 中多个线程对非重叠非连续索引的无锁处理
- python - 如何使用 move_base 包在 ros 中移动多个机器人
- ssh - vmware esxi 不允许的 Mac 地址
- java - 修改什么使其使用方法实现模块化
- javascript - 在javascript中执行函数后如何执行一些语句
- php - Laravel Spark - 定义免费计划
- html2canvas - html2canvas 边框图像不呈现,替代方案?