Can someone explain this syntax (in REPL)?
include("Filename.jl")
using .Filename
I can sort of understand the first, but what is the . in front of Filename mean?
Can someone explain this syntax (in REPL)?
include("Filename.jl")
using .Filename
I can sort of understand the first, but what is the . in front of Filename mean?
I’ve not used that format before, but a quick read of the module documentation indicates that you can access module hierarchy’s using the “.” notation. LOL, I didn’t even know you could have submodules !
https://docs.julialang.org/en/v1/manual/modules/#Submodules-and-relative-paths
Actually the filename has nothing to do with using .Filename
. Filename
is expected to be a module defined in the file.
You can see the effect of using .ModuleName
without needing to worry about include
at all. Here’s a simple example:
julia> module MyModule
export foo
foo() = println("hello world")
end
Main.MyModule
Here I’ve defined a module which exports one function (foo
). But I’ve only defined that module, I haven’t using
-ed it, so I don’t yet have access to that exported function:
julia> foo
ERROR: UndefVarError: foo not defined
To get foo
into my current namespace, I need using
. But if I just do using MyModule
, it doesn’t work:
julia> using MyModule
ERROR: ArgumentError: Package MyModule not found in current path:
That’s because using MyModule
tells Julia to look for some module with that name in a file in the LOAD_PATH
or in the current package environment. That’s not what we want–we already defined that module.
Instead, we can do using .MyModule
which says "find a module named MyModule
which is already defined in the current namespace (in this case, in Main
), and activate it:
julia> using .MyModule
julia> foo()
hello world
You can add additional dots to activate modules in a parent module:
julia> module A
export bar
bar() = println("bar")
module B
using ..A
function f()
println("Calling `bar` which was defined in the parent module `A`:")
bar()
end
end
end
Main.A
julia> A.B.f()
Calling `bar` which was defined in the parent module `A`:
bar