So if I have module A defining a struct, then with "using" import the struct to mo

So if I have module A defining a struct, then with “using” import the struct to module B, and define a function on that struct. Then in module C “using” import both module A and B. When I try to run the function on the struct I get a method error. Is there any way to make the struct imported from A and methods from B apply to each other appropriately? MWE:
exampleimport.jl
module ExampleImport
mutable struct TestStruct
val::Int
end
export TestStruct
end

exampleimport2.jl
module ExampleImport2
include("exampleimport.jl")
using .ExampleImport
function teststructmod!(x::TestStruct)
x.val = 10
end
export teststructmod!
end

example.jl
include("exampleimport.jl")
include("exampleimport2.jl")

using .ExampleImport2
using .ExampleImport

tmp = TestStruct(2)

teststructmod!(tmp)

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

You can make this exact example work by using Reexport.jl. Keep module 1 the same. In 2 use

using Reexport
@reexport using .ExampleImport

and then remove include("exampleimport.jl") and using .ExampleImport from example.jl

2 Likes

You may find interesting this tutorial on modules and packages:

https://syl1.gitbook.io/julia-language-a-concise-tutorial/language-core/11-developing-julia-packages

2 Likes