5

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?

Aayaan Sahu
  • 125
  • 1
  • 8

2 Answers2

4

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)

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
1

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

Simply remove the following line from MatrixFunctions.jl:

include("VectorFunctions.jl")
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • It then gives me the error `ERROR: LoadError: UndefVarError: VectorFunctions not defined`. It might help to say that there is a function named `dot_product` within **VectorFunctions.jl** and within **MatrixFunctions.jl**, I am calling it using `VectorFunctions.dot_product(v1, v2)`. – Aayaan Sahu Jul 31 '21 at 07:05
  • maybe you have some other code somewhere. It is enough to load code with a module once. You do it twice and hence get a warning. – Przemyslaw Szufel Jul 31 '21 at 08:43