How to organize modules in a composable way?

mj2984
Sat, 13 Dec 2025 19:14:58 +0000
General Usage

Suppose I have a module module_init that defines a type init_struct, and I am now trying to make multiple modules in different projects that build on top of this init_struct. For example

./project_init/src/module_init.jl contains

module module_init
export init_struct,do_something
struct init_struct end
function do_something(init_struct)
     #do something
end
end

Then I have a few projects: eg project_sub1 and project_sub2 that have their code like.

./project_sub1/src/sub1_module.jl contains

module module_sub1
export sub1Struct,performSub1Action
include(raw"./project_init/src/module_init.jl")
using .module_init
struct sub1Struct{T}
     init::init_struct
     sub1::T
end
functon performSub1Action(input::sub1Struct{T}) where {T}
     do_something(input.init)
     # Do other actions for sub1
end
end

./project_sub2/src/sub2_module.jl contains

module module_sub2
export sub2Struct,performSub2Action
include(raw"./project_init/src/module_init.jl")
using .module_init
struct sub2Struct{T}
     init::init_struct
     sub2::T
end
functon performSub2Action(input::sub1Struct{T}) where {T}
     do_something(input.init)
     # Do other actions for sub1
end
end

This is not ideal because the composability is gone. I cannot do something like

include(“./project_init/src/module_init.jl”)
using .module_init
include("./project_sub1/src/sub1_module.jl")
using .module_sub1
init = init_struct()
sub1 = 3
data = sub1Struct(init,sub1)

Since the init_struct inside the sub1 module has a different namespace module_sub1.module_init

I can try to import and re-export, but then I will not be able to easily use module_sub1 and module_sub2 simultaneously.

How should I organize my code such that I am able to do something like

using .module_init
using .module_sub1
using .module_sub2

init = init_struct()
sub1 = 3
sub2 = 4
data1 = sub1Struct(init,sub1)
data2 = sub1Struct(init,sub2)

Similar to how I can use packages that is able to do this? Where I can do something like using module_init on both module module_sub1 and module_sub1.

Is packages the only way to do this? Also, what is the difference between a package and a module. I am unable to find the right keywords to search for documentation in this area.

7 posts - 5 participants