Modules vs Packages
Modules are used to group multiple functions and definitions together. Packages group multiple modules together with various metadata.
Create a custom module
Open up a new .jl file to create your own module. Here I named the file “testModule.jl”. In the file, one can group functions and/or other definitions together. Two functions are grouped by the module myModule
. Only myfunction
is exported.
module myModule
export myfunction
function myfunction()
println("Hello, friend")
end
function mysecretfunction()
println("Hello, secret friend")
end
end
Load modules
To load modules, use either using
or import
. using
will only make export lists accessible without specifying it with the module name. For example, mysecretfunction()
will return an error.
include("testModule.jl")
using .myModule
mysecretfunction()
However, we can access it with
myModule.mysecretfunction()
julia> Hello, secret friend
Using import
,
include("testModule.jl")
import .myModule
myModule.myfunction()
julia> Hello, friend
myModule.mysecretfunction()
julia> Hello, secret friend
myfunction()
without a module name will give you an UndefVarError
.
Reference
https://en.wikibooks.org/wiki/Introducing_Julia/Modules_and_packages