Hello there
I’m trying to understand how to use modules probably, but I’m finding the way they work and how they are loaded kinda messing. The following example yields would I expect on the first run, but every subsequent run, I get several warnings
module Mstruct
export point2D, point3D
mutable struct point2D
x::Int32
y::Int32
end
mutable struct point3D
x::Int32
y::Int32
z::Int32
end
end
module M1
using ..Mstruct
export add2D
function add2D(p::point2D)
return p.x + p.y
end
end
module M2
using ..Mstruct
export add3D
function add3D(p::point3D)
return p.x + p.y + p.z
end
end
using .Mstruct, .M1, .M2
function main()
p1 = point2D(1,1)
println(typeof(p1))
p2 = point3D(1,1,1)
println(typeof(p2))
val = add2D(p1)
println(val)
val = add3D(p2)
println(val)
end
main()
First run I get:
point2D
point3D
2
3
but every following run yields:
WARNING: replacing module Mstruct.
WARNING: replacing module M1.
WARNING: replacing module M2.
WARNING: using Mstruct.point2D in module Main conflicts with an existing identifier.
WARNING: using Mstruct.point3D in module Main conflicts with an existing identifier.
WARNING: using M1.add2D in module Main conflicts with an existing identifier.
WARNING: using M2.add3D in module Main conflicts with an existing identifier.
point2D
point3D
2
3
Now I understand this is happening because the workspace has already loaded the modules, but how do I avoid this? Naturally its not a problem, if I “Stop REPL” between every run, but I would definitely prefer not to have to do that. On the whole I think “why” is more important to me than “how do I fix it”, as I find modules general unintuitive.
Another problem I have been having, which i think comes from the same lack of understanding, is that when I define the input of functions in modules (for example here, where add2D(p::point2D)), the base, and module name gets appended to the struct, so the function fails to run, because the objective is defined as “Main.Mstruct.point2D” and not as “point2D”.