Shared definitions across modules

Hello, and welcome to Julia!

Regarding your question: have you thought about idea not to use modules at all? Unlike python or other similar languages Julia is less restrictive and it is quite common to wrap all package in one single module and plainly include all other files. It helps multiple dispatch and minimize number of issues like yours.

But if you insist on using modules, then you can do the following: define your structure in one of the module and then you can import it with the help of using and .. syntax. When you are prepending ., .., ... before the name of the module, it is similar to navigation in file system, so .. means “module which is defined in parent module”.

So it will look like this

# file defs.jl
module A
struct Foo end
end  # module

# file misc.jl
module B
using ..A: Foo

x = Foo()
end

# file main.jl
include("defs.jl")
include("misc.jl")

Thirdly, your structure uses abstract types (Array is an abstract type), so it’s better to change it to a concrete type or you can hurt perfrormance. See Performance tips for more info.

Fourthly, please, when you are posting in discourse, close your code in triple backticks: item 2 of "make it easier to help you.

5 Likes