Julia 中的子模块内部依赖
Submodule intra-dependencies in Julia
我正在尝试创建具有以下布局的程序包:
MyPkg
├── MyPkg.jl
├── Foo
│ ├── Foo.jl
│ └── another_file.jl
└── Bar
├── Bar.jl
└── yet_another_file.jl
我的主包模块看起来像这样:
# MyPkg.jl
module Pkg
include("./Foo/Foo.jl")
using .Foo: FooStuffA, FooStuffB
export FooStuffA, FooStuffB
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
end
当 Foo
需要在某些函数参数中 Bar
中定义的类型(特别是 struct
)时,就会出现问题。我不确定如何导入这种类型。我已经尝试了 include("../Bar/Bar.jl")
、using Bar/.Bar/..Bar
、Foo
子模块内部、子模块外部等的所有组合
# Foo.jl
module Foo
# what am I missing here?
function print_bar_struct(bar::BarStruct)
@show bar
end
end
有什么建议吗?
您是否尝试过使用 module.struct 全名引用结构,如 Bar.BarStruct 中那样?
对于结构和枚举,导出函数似乎不如函数名那么好用,但使用 Module.Struct 类型语法通常有效。
这应该有效
# MyPkg.jl
module MyPkg
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
include("./Foo/Foo.jl")
using .Foo: FooStuffA, FooStuffB
export FooStuffA, FooStuffB
end
# Foo.jl
module Foo
using ..MyPkg: BarStruct
function print_bar_struct(bar::BarStruct)
@show bar
end
end
说明:请记住,include 语句实质上是将源文件中的代码复制+粘贴到给定行的模块中。因此,当编译器查看所有符号的引用(从文件顶部到底部读取)时,在 include("./Foo/Foo.jl")
出现的位置,它需要知道 BarStruct 存在并且可以访问在当前模块(即 MyPkg)中,它位于这个重新排列的布局中。
所以只看 MyPkg
的前半部分
# MyPkg.jl
module MyPkg
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
当编译器到达此处的最后一行时,BarStruct
、BarStuffC
、BarStuffD
是带入 MyPkg
命名空间的符号(https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage-1).
当我们到达 include("./Foo/Foo.jl")
行时(也就是此时将此源文件复制粘贴到当前模块中),我们需要在该模块的父命名空间中引用 BarStruct
,即, ..BarStruct
我正在尝试创建具有以下布局的程序包:
MyPkg
├── MyPkg.jl
├── Foo
│ ├── Foo.jl
│ └── another_file.jl
└── Bar
├── Bar.jl
└── yet_another_file.jl
我的主包模块看起来像这样:
# MyPkg.jl
module Pkg
include("./Foo/Foo.jl")
using .Foo: FooStuffA, FooStuffB
export FooStuffA, FooStuffB
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
end
当 Foo
需要在某些函数参数中 Bar
中定义的类型(特别是 struct
)时,就会出现问题。我不确定如何导入这种类型。我已经尝试了 include("../Bar/Bar.jl")
、using Bar/.Bar/..Bar
、Foo
子模块内部、子模块外部等的所有组合
# Foo.jl
module Foo
# what am I missing here?
function print_bar_struct(bar::BarStruct)
@show bar
end
end
有什么建议吗?
您是否尝试过使用 module.struct 全名引用结构,如 Bar.BarStruct 中那样?
对于结构和枚举,导出函数似乎不如函数名那么好用,但使用 Module.Struct 类型语法通常有效。
这应该有效
# MyPkg.jl
module MyPkg
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
include("./Foo/Foo.jl")
using .Foo: FooStuffA, FooStuffB
export FooStuffA, FooStuffB
end
# Foo.jl
module Foo
using ..MyPkg: BarStruct
function print_bar_struct(bar::BarStruct)
@show bar
end
end
说明:请记住,include 语句实质上是将源文件中的代码复制+粘贴到给定行的模块中。因此,当编译器查看所有符号的引用(从文件顶部到底部读取)时,在 include("./Foo/Foo.jl")
出现的位置,它需要知道 BarStruct 存在并且可以访问在当前模块(即 MyPkg)中,它位于这个重新排列的布局中。
所以只看 MyPkg
# MyPkg.jl
module MyPkg
include("./Bar/Bar.jl")
using .Bar: BarStruct, BarStuffC, BarStuffD
export BarStruct, BarStuffC, BarStuffD
当编译器到达此处的最后一行时,BarStruct
、BarStuffC
、BarStuffD
是带入 MyPkg
命名空间的符号(https://docs.julialang.org/en/v1/manual/modules/#Summary-of-module-usage-1).
当我们到达 include("./Foo/Foo.jl")
行时(也就是此时将此源文件复制粘贴到当前模块中),我们需要在该模块的父命名空间中引用 BarStruct
,即, ..BarStruct