Module 在Julia中的另一个模块中包含一个模块

Module 在Julia中的另一个模块中包含一个模块,module,namespaces,julia,Module,Namespaces,Julia,我在文件中定义了一个模块。这个模块1定义了我在主脚本中使用的结构和函数。我使用include(“module1.jl”)将此模块包含在另一个父模块中,以便父模块可以使用module1中的结构和函数。但是,我的名称空间有问题。以下是单个文件中的一个示例: #this would be the content of module.jl module Module1 struct StructMod1 end export StructMod1 function fit

我在文件中定义了一个模块。这个模块1定义了我在主脚本中使用的结构和函数。我使用include(“module1.jl”)将此模块包含在另一个父模块中,以便父模块可以使用module1中的结构和函数。但是,我的名称空间有问题。以下是单个文件中的一个示例:

#this would be the content of module.jl
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)
    end
    export test
end

using .Parent, .Module1

s = StructMod1
test(s)



ERROR: LoadError: MethodError: no method matching test(::Type{StructMod1})
Closest candidates are:
  test(::Main.Parent.Module1.StructMod1)
如果我删除父级中的模式包含并使用..Module1使其从封闭范围加载,则会出现此错误

ERROR: LoadError: MethodError: no method matching test(::Type{StructMod1})

    Closest candidates are:
      test(::StructMod1) at ...

在您的示例中,
s
是类型对象,而不是类型为
StructMod1
的对象。为了使
s
成为后者,您需要调用该类型的构造函数。因此,您应该编写
s=StructMod1()
而不是
s=StructMod1


您可以阅读有关类型作为第一类对象的更多信息。

何时出现错误,何时加载模块或何时调用函数测试?这两个错误都发生在调用测试时。David回答如下
::Type{StructMod1}
表示您传递了类型,而不是实例。实例将是
::StructMod1
。错误消息只是说没有一个
test
方法接受类型为
type
Yes的参数!那是个打字错误。我的问题是使用StructMod1()定义结构,然后从另一个模块中调用fit函数。我将用更清晰的代码开始一个新问题。顺便说一句,Hyperopt软件包做得很好!它在我的模拟软件中非常有用,这是一个输入错误。我的代码中仍然存在相同的问题,我将用一个更好的示例来打开一个新问题。