Creating submodules

When a module becomes too large, it may make sense to split it into smaller parts so that it's easier to develop and maintain. One way to solve this problem is to create submodules.

Creating submodules is convenient as they are just defined within the scope of the parent module. Let's say we organize the Calculator module with two submodules—Mortgage and Banking. These submodules can be defined in separate files and can be included directly into the parent module. Consider the following code:

# Calculator.jl
module Calculator

include("Mortgage.jl")
include("Banking.jl")

end # module

Submodules, just like regular modules, are also defined using module blocks. The source code for Mortgage looks just like a regular module definition:

# Mortgage.jl
module Mortgage

# mortgage related source code

end # module

Because the source code from Mortgage is included inside the Calculator module block, it forms a nested structure. The usage of submodules is the same as that of any regular module, except that you have to reference them via the parent module. In this case, you'd use Calculator.Mortgage or Calculator.Banking.

Using submodules is an effective way to separate code for larger codebases. Next, we will go over how to organize source code in a module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.42.196