Julia: How can I access the type from inside the module?

Suppose I would like to access enlobing type from within a module. To be specific:

file Englobing.jl

using myModule
type MyType
    a::Float64
    b::Vector{Float64}
end

t = MyType( 1., [ 1., 2. ] )

x = [ .5, .5 ]

myFunc( x, t )

file myModule.jl

module myModule

export myFunc

    function myFunc( x::Vector{Float64}, z::MyType )
        [ operations ]
    end

end

In this case, I would like to have access to the MyType type from the myModule module without using globals.

+3
source share
2 answers

Option 1:

You can also export types. For instance. if it Englobing.jlwas a module, you could:

export MyType

Then in your file myModule.jlyou can:

using Englobing

Option 2

If Englobing.jlit was not a module (which it is not currently written as), you can simply use

include("Englobing.jl")

inside myModule.jl.

, , Englobing.jl - (, , ..) myModule.jl, myModule.jl - Englobing.jl, ​​, , , , .
+3

, " MyType". , .

, . module A using B, module B using A. , , . , , , , .

- . , :

module Outer    # or just Main, the default

type MyType
    ...
end

module myModule

import ..MyType   # imports from the outer module

end

using .myModule

end

, myModule include , MyType. .

+2
source

Source: https://habr.com/ru/post/1651701/


All Articles