How to remove the WARNING: replace module <module> in Julia

Viewed 565

I have a couple Julia files within a directory, namely

FolderName
|
|---main.jl
|---VectorFunctions.jl
|---MatrixFunctions.jl

The following is the structure for the files:

VectorFunctions.jl

module VectorFunctions
    # functions...
end

MatrixFunctions.jl

include("VectorFunctions.jl")

module MatrixFunctions
    # functions...
end

main.jl

include("VectorFunctions.jl")
include("MatrixFunctions.jl")

# Using the functions defined in both files...

Running the code with julia main.jl in my terminal, I get the following:

WARNING: replacing module VectorFunctions.

=== Desired Output ===

If I remove the include to VectorFunctions.jl in main.jl, the error disappears (I suspect because VectorFunctions is not being included again). Is there a way to get around this WARNING and still keep my my additional include in main.jl?

2 Answers

The answer by Przemysław is correct. Note that include works as if you did copy-paste of the code from one place to another. So - using include you act as-if you copy-pasted the same code twice into your program.

However, note that:

  1. If you turned VectorFunctions into a package (here is an instruction how to do it), then instead of include, you would write using VectorFunctions and you could write it as many times as you like without a problem.
  2. Alternatively you could write the following code instead of include("VectorFunctions.jl"), note though that it is not something that normally you would do (the approach that Przemysław describes or creating a package are recommended); the approach below assumes that the only thing that VectorFunctions.jl file holds is VectorFunctions module:
if isdefined(@__MODULE__, :VectorFunctions)
    VectorFunctions isa Module || error("VectorFunctions is present and it is not a Module")
else
    include("VectorFunctions.jl")
end

(again - be warned that this is not something that is recommended to do; I just give it to show you that it is possible to avoid reloading the same module into the current module twice)

Your module VectorFunctions is defined two times what obviously does not make sense.

Simply remove the following line from MatrixFunctions.jl:

include("VectorFunctions.jl")
Related