liveBook · Manning

Module

As explained in Julia as a Second Language

A module is a Julia namespace that groups related functions, types, constants, and other definitions.

A Julia module groups related functions, types, constants, and other definitions in a namespace. Separate namespaces let functions share a name without interfering with one another. Define a module with `module` and `end`, and access a definition using its module name, as in `Cylinder.volume`. section 16.1.2 section 4.4

Listing 16.3 Functions with the same name in different modules
module Cylinder
    volume(r,h) = π*r^2*h
end
 
module Cone
    volume(r, h) = π*r^2*h/3
end

Cylinder and Cone each define a `volume` function; call them as `Cylinder.volume(2.5, 3)` and `Cone.volume(2.5, 3)`. section 16.1.2

A package and a module are different things. A package bundles source code, resources, and metadata; a module is a Julia language construct. A package defines a module of the same name: `using Dates` loads the Dates module from the Dates package. A practical organization is one module per package. section 16.1.2

Use a standard-library module

Run `using Statistics` to use ready-made functions such as `mean`, `median`, and `std`. In the REPL, enter `?` for documentation mode, or type `Statistics.` and press Tab twice to explore completions. section 4.4

For a growing module, split definitions across files and call `include` inside the module. The included definitions are evaluated in that module. Unlike C/C++ `#include`, Julia’s `include` is a regular function call that returns a result. section 16.2.2 section 16.4

Listing 16.6 ToyGeometry/src/ToyGeometry.jl
module ToyGeometry

include("volume.jl")
include("trig.jl")

end # module

ToyGeometry includes separate files for volume and trigonometry definitions within one module. section 16.2.2

Julia’s bundled functionality largely belongs to `Base`; definitions made at the REPL normally belong to `Main`. To add methods to an existing Base function, import it first. For example, `import Base: -, +` lets angle-specific methods extend those operators rather than creating separate functions that shadow them. section 9.3.2

Use `export sphere_volume` to identify an exported function. A Java-style `public function` declaration is not Julia syntax. section 16.4

How Julia as a Second Language covers it

How deep it goes
Explains module definitions, package relationships, included files, and imported function extension through worked examples.
Examples
Uses cylinder and cone volume functions, Statistics REPL commands, and a geometry package split across source files.
What you can do after
Build a Julia module from scratch and organize its definitions across included files.
How it connects
Connects namespaces with packages, imports, function methods, and shadowing.

Also explained in

Compare how 36 books explain module →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.