Modules¶
A namespace is a set of functions or scripts that can "see" the same objects.
In Julia, namespaces are created by modules.
A module
is created by
julia> module Foo
export g
f(x) = x ^ 2;
g(x) = x / 2;
end
Main.Foo
Even what you run in the REPL lives in a module called Main
.
This is why the module
that we just created is Main.Foo
(not just Foo
).
It is a sub-module of Main
.
To access the objects inside of Foo
we need to import the module:
# We did not import `Foo`, so we get an error.
julia> f(2)
ERROR: UndefVarError: f not defined
Stacktrace:
[1] top-level scope at REPL[3]:1
# This imports `Foo`
julia> using .Foo
julia> Foo.f(2)
4
We still cannot just type f(2)
because Foo
did not export f
Only objects that are exported can be called without the module name as a qualifier.
julia> g(2)
1