Julia Submodules
A submodule is a module placed inside another module. It groups related names under the outer module.
A submodule sits inside an outer module
Library holds Shelf inside it. The full path reaches the book function.
module Library
module Shelf
function book()
return "Maps"
end
end
end
println(Library.Shelf.book())A dot using line helps code inside the outer module
Workshop uses its inner Tools module. Its work function then calls hammer by its short name.
module Workshop
module Tools
export hammer
function hammer()
return "hammer ready"
end
end
using .Tools
function work()
return hammer()
end
end
println(Workshop.work())A dot path can bring a submodule into the current code
Garden holds Seeds inside it. A dot path brings plant into the current code.
module Garden
module Seeds
export plant
function plant()
return "tomato"
end
end
end
using .Garden.Seeds
println(plant())Example
module Store
module Stock
export items
function items()
return 7
end
end
using .Stock
function total()
return items() + 3
end
end
println(Store.total())
println(Store.Stock.items())Key points
- Submodule: a module nested inside another module.
- Nested path: an inner name is reached through the outer module, then the inner module, then the name.
- Inner grouping: a submodule keeps a small set of related names together.
- Dot form: a using line that starts with a dot reaches a module in the current code.
- Inside access: inside the outer module, a dot using line lets outer code call inner names by their short names.
In this example, Store holds Stock inside it and uses its inner module. The total function calls items by its short name and adds 3. The first line prints 10. The second line reaches items by its full path and prints 7.
Note
an inner module can still be reached by its full path even when its names are also brought in by a dot using line.
Try it Yourself
Edit the code, then press Run.
Running is simulated · JuliaHub
Exercise
What does using .Inner do inside module Outer?