How do I structure this correctly in my package?

Hello!

I have made a small package here:

In this package I have a folder structure such as:

- src
        - geometry.jl
        - main.jl
        - main_package.jl
        - math_vectors.jl

In which I try to split stuff into modules, because this is what I like from Rust.

Basically math_vectors define some basic math functionality I want, geometry uses this functionality, main_package bundles the two together and they are then called in main:

include("main_package.jl")
using .main_package


a = Vec(1.,2.,3.)
b = Vec(2.,4.,6.)

println(a + b)
println(a - b)
println(a / b)
println(a * b)

println(ℕ²(a))
println(â„•(a))

v1 = Vec(0.,0.,0.)
v2 = Vec(1.,1.,1.)
vline = draw_line(v1,v2,0.4)
println(vline)

When I do like this it works fine to call “Vec”, but when I try to use “draw_line” from geometry.jl:

ERROR: MethodError: no method matching draw_line(::Vec{Float64, Float64, Float64}, ::Vec{Float64, Float64, Float64}, ::Float64)
Closest candidates are:
  draw_line(::Main.geometry.math_vectors.Vec, ::Main.geometry.math_vectors.Vec, ::Number) at ~/.julia/dev/SPH_jl/src/geometry.jl:7
Stacktrace:
 [1] top-level scope
   @ ~/.julia/dev/SPH_jl/src/main.jl:18

Suddenly it does not accept “Vec” from “main space”, but wants a specific version. How do I fix this?

Kind regards

The easiest way is not to split your package in modules but just use one main module that includes the other files.

2 Likes

It sounds like the issue is that each Module defines its own function draw_line.
What you probably intended was to add methods to a single function draw_line. That would work, for example, like this:

module A
  function foo(x :: Int) end
end

using .A

function A.foo(x :: Float64) end

# or
import A: foo
function foo(x :: Float64) end

end

That is not the issue

Each module has some functionality, the functionality of math_vectors need to present in both main and geometry. Then a conflict occurs in regards to the same type, one defined in main and one defined in the geometry file. That is what confuses me

Kind regards

That works. But is that really the only way?

Kind regards

It is the most practical way while the functionality of these modules does not justify a new package. When that happens, creating a new package is nice because then the dependency managing is taken care by the package manager. (Modules that are not packages are less useful in Julia than in other languages).