0

我在 SO 上发现了类似的问题,但似乎都没有给出适合我的情况的答案。

我有几个模块,在其中一个模块中我创建了一个可变结构,我希望能够在其他模块中使用它。所有文件都在同一级别:

  • file_module_A.jl
  • file_module_B.jl
  • file_module_C.jl

在 file_module_A.jl 中:

module A
   mutable struct MyType
      variable
   end
end

在 file_module_B.jl 中:

module B
    # I need to import MyType here
end

在 file_module_C.jl 中:

module C
    # I need to import MyType here
end

我尝试了以下方法但没有成功:

  • 直接使用:using .A不起作用
  • 我不能使用:include("./file_module_A.jl")在 B 和 C 中,因为当它们相互交互时,我得到错误 can't convert from Main.BA to Main.CA because includeinclude a copy of the entire code

有任何想法吗?提前致谢!

4

1 回答 1

3

你需要使用using ..A. using .A意味着A在当前模块中查找(在下面的示例中),如果您在 REPL 中运行示例B,您需要额外的.来提升一个模块级别:Main

module A
    mutable struct MyType
        variable
    end
end

module B
    using ..A: MyType
end
于 2019-01-15T11:12:04.020 回答