Organizing modules. Is it OK to organize a project using several modules in Julia?

I would suggest that you start playing around with module structures without using include or export, just to get a sense of how they work. Here’s an example of a module which is itself composed of 3 submodules, and in which the GameRunner and PlayerModule modules can both correctly access the Board type from BoardModule:

julia> module GameAI
         module BoardModule
           struct Board end
         end
         
         module GameRunner
           using ..BoardModule: Board

           run(b::Board) = println("running: $b")
         end
         
         module PlayerModule
           using ..BoardModule: Board
           
           move(b::Board) = println("moving on: $b")
         end
       end
Main.GameAI

julia> using .GameAI

julia> board = GameAI.BoardModule.Board()
Main.GameAI.BoardModule.Board()                                                                                                                                  
                                                                                                                                                                 
julia> GameAI.GameRunner.run(board)
running: Main.GameAI.BoardModule.Board()                                                                                                                         
                                                                                                                                                                 
julia> GameAI.PlayerModule.move(board)
moving on: Main.GameAI.BoardModule.Board()   

You can move any of those modules into their own files and replace the module definition with include("module.jl") as you see fit, but it’s not necessary in order to understand the module organization.

The other rule of thumb is: a .jl file should not be included more than once in your code.

6 Likes