Warning about variable conflict from module export?

This may not work in v"1.10", got a warning on name conflicts.

julia> module Foo
           x = 1
           y = 2
       
           for n in names(@__MODULE__; all=true)
               if Base.isidentifier(n) && n ∉ (Symbol(@__MODULE__), :eval, :include)
                   @eval export $n
               end
           end
       end
Main.Foo

julia> using .Foo

julia> x
1

julia> module Foo
           x = 1000000
           y = 2
       
           for n in names(@__MODULE__; all=true)
               if Base.isidentifier(n) && n ∉ (Symbol(@__MODULE__), :eval, :include)
                   @eval export $n
               end
           end
       end
WARNING: replacing module Foo.
Main.Foo

julia> using .Foo
WARNING: using Foo.x in module Main conflicts with an existing identifier.

julia> x
1

That’s because you’re redefining the module and including it a second time, after already having used one of the imports from the first time.

This happens even if you do manual exports, i.e. if you paste the following code into the REPL twice:

module Foo; export x; x = 1; end
using .Foo
x

If you want to develop a module, the best thing to do is to put it into a package (Best practise: organising code in Julia - #2 by stevengj) and use Revise.jl.

PS. I moved this to new topic because your question has nothing to with the original thread. In general, be cautious about “necroposting” — posting new questions on threads that were resolved years ago.

2 Likes

Thank you for the clarification! I understand now. I also apologize for the necroposting and will be more mindful of that in the future.