Interdependent files in module


#1

Say I have the following module definition:

module Foo
include("bar.jl")
include("baz.jl")
include("qux.jl")
end

bar.jl uses a function provided by qux.jl. What is the julian way to handle this sort of intermodular dependency? Should we just order the files in an order that satisfies all requirements, or is there a neater approach?


#2

That’s the simplest solution, if you want to keep everything in the same module.


#3

Or add a function definition with no method at the beginning of the module

function f end

This way you can have bar.jl use baz.jl functions and the other way around. See https://docs.julialang.org/en/latest/manual/methods/#Empty-generic-functions-1.


#4

For functions the order should not matter, if you only call them in the function body, e.g.

julia> module Foo
       bar() = baz()
       baz() = "Hello, world!"
       end
Foo

julia> Foo.bar()
"Hello, world!"

The ordering matters however if you use the function in the function signature, e.g.

julia> module Foo
       bar(::typeof(baz)) = baz()
       baz() = "Hello, world!"
       end
ERROR: UndefVarError: baz not defined