#MonthOfJulia Day 10: Modules


Modules allow you to encapsulate your code and variables. In the words of the Julia documentation:

Modules in Julia are separate global variable workspaces. Modules allow you to create top-level definitions without worrying about name conflicts when your code is used together with somebody else’s. Within a module, you can control which names from other modules are visible (via importing), and specify which of your names are intended to be public (via exporting). Modules, Julia Documentation

To illustrate the concept, let’s define two new modules:

julia> module AfrikaansModule
       __init__() = println("Initialising the Afrikaans module.")
       greeting() = "Goeie môre!"
       bonappetit() = "Smaaklike ete"
       export greeting
Initialising the Afrikaans module.
julia> module ZuluModule
       greeting() = "Sawubona!"
       bonappetit() = "Thokoleza ukudla"

If an __init__() function is present in the module then it’s executed when the module is defined. Is it my imagination or does the syntax for that function have an uncanny resemblance to something in another popular scripting language?

The greeting() function in the above modules does not exist in the global namespace (which is why the first function call below fails). But you can access functions from either of the modules by explicitly giving the module name as a prefix.

julia> greeting()
ERROR: greeting not defined
julia> AfrikaansModule.greeting()
"Goeie môre!"
julia> ZuluModule.greeting()

The Afrikaans module exports the greeting() function, which becomes available in the global namespace once the module has been loaded.

julia> using AfrikaansModule
julia> greeting()
"Goeie môre!"

But it’s still possible to import into the global namespace functions which have not been exported.

julia> import ZuluModule.bonappetit
julia> bonappetit()
"Thokoleza ukudla"

In addition to functions, modules can obviously also encapsulate variables.

That’s pretty much the essence of it although there are a number of subtleties detailed in the official documentation. Well worth a look if you want to suck all the marrow out of Julia’s modules. As usual the code for today’s flirtation can be found on github.

Categorically Variable