Referencing symbols and functions between modules and sub-modules

A module can access its sub-modules using the regular using or import statements. In fact, a sub-module does not work any differently than an external package, except how it is being referenced.

Perhaps we can recall the example from Chapter 2Modules, Packages, and Type Concepts. Back then, we created a Calculator module that defines two interest rate-related functions and a Mortgage sub-module that defines a payment calculator function. The Calculator module file has the following source code:

# Calculator.jl
module Calculator

include("Mortgage.jl")

export interest, rate

function interest(amount, rate)
return amount * (1 + rate)
end

function rate(amount, interest)
return interest / amount
end

end # module

Furthermore, the sub-module contains the following code:

# Mortgage.jl
module Mortgage

function payment(amount, rate, years)
# TODO code to calculate monthly payment for the loan
return 100.00
end

end # module

Let's look into how to reference functions and symbols from a sub-module and vice versa.

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

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