liveBook · Manning

Import/export syntax

As explained in Julia as a Second Language

An export statement is Julia syntax that makes selected bindings from a module available without requiring the module-name prefix.

Export statements make selected functions and types from a Julia module available without repeating the module name. They turn awkward calls such as ToyGeometry.sine into the simpler sine form while keeping other bindings module-qualified. section 16.3

Exporting custom trigonometric functions

Add `export sine, cosine` inside the module, then load it with `using ToyGeometry`. The REPL can call `sine(π/2)` and `cosine(π)` directly; without the export, `sine(π/2)` raises an undefined-variable error while `ToyGeometry.sine(π/2)` works. section 16.3

export sine, cosine    #1
 
"""
    sine(x)
Calculate the sine of an angle `x` given in radians
"""
function sine(x::Number)
    n = 5
    total = 0
    for i in 0:n
        total += (-1)^i*x^(2i+1)/factorial(2i + 1)
    end
    total
end
 
function cosine(x::Number)
   n = 5
   mapreduce(+, 0:n) do i
        (-1)^i*x^(2i)/factorial(2i)
   end
end

Shows the export declaration beside the complete `sine` and `cosine` implementations. section 16.3

The exported functions can then participate in ordinary Julia workflows:

  • Compare custom and built-in trigonometric functions numerically. section 16.3
  • Plot both implementations with CairoMakie using unqualified function names. section 16.3

How Julia as a Second Language covers it

How deep it goes
Explains the declaration and demonstrates its effect, but does not descend into module-loading internals.
Examples
Uses exported sine and cosine functions, REPL calls, built-in comparisons, and a CairoMakie plot.
What it assumes
Assumes familiarity with Julia modules, packages, using, and basic function calls.
What you can do after
Apply it by declaring public module functions and calling them directly after using the package.
How it connects
Connects modules, packages, using, the REPL, Revise, and function reuse.

Also explained in

Compare how 5 books explain import/export syntax →

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.