Change in behavior when loading a module twice

Magnus_Lilledahl
Mon, 19 Aug 2024 12:35:30 +0000
New to Julia

I have a module Mod1 stored in a file Mod1.jl and a module Mod2 stored in a file Mod2.jl. ‘Mod1’ exports a function userfun that uses a function foo which is defined in Mod2. Mod2 also defines some new types.

module Mod1
	export userfun

	include("Mod2.jl")
	using .Mod2

	function userfun()
		foo()
	end
end

module Mod2
	export foo

	using InteractiveUtils # for subtypes function

	abstract type MyAbstract end
	struct MyStruct <: MyAbstract end

	function foo()
		subtypes(MyAbstract)
	end
end

If I now write in a fresh REPL

include("Mod1.jl")
using .Mod1
userfun()

I get

1-element Vector{Any}:
 Main.Mod1.Mod2.MyStruct

as expected. But if I now (say, a user accidentaly) repeat the inclusion of the module and call userfun

include("Mod1.jl")
userfun()

I get

Any[]

Seemingly MyAbstract suddenly has now subtypes. If I afterwards redo using .Mod1 I get the same empty array.

Can anyone explain why the behavior changes after I include the module twice (This is of course not the actual code, I have tried to distill the problem as much as possible)?

5 posts - 3 participants