May 14, 2021 Julia
2. Summary of the module usage methods
5. Default top-level declarations and bare modules
Julia's module is a separate global variable workspace. I
t is limited by synth
module Name ... end
ends.
Inside the module, you can control whether the naming of other modules is
import
or you can indicate whether the name of the module is public (through
export
The following example shows the main features of the module. This example is just a demonstration:
module MyModule
using Lib
using BigLib: thing1, thing2
import Base.show
importall OtherLib
export MyType, foo
type MyType
x
end
bar(x) = 2x
foo(a::MyType) = bar(a.x) + 1
show(io, a::MyType) = print(io, "MyType $(a.x)")
end
Note that the above example does not have code for the indented module body because the overall indentation is not necessary.
This module defines the
MyType
and two functions.
foo
function and
MyType
are exported, so they can be imported into other modules.
bar
is
MyModule
The
using Lib
indicates that the
Lib
module can be used to resolve naming when needed. I
f a global variable is not defined in the current module,
Lib
export's variable and imports it in when found.
Whenever this global variable is used in the current module, the definition of the variable
Lib
is looked for.
Statement
using BigLib: thing1, thing2
using BigLib.thing1, BigLib.thing2
import
keyword supports all the same syntax as
using
but only one name can be operated on at a time. I
t does not add modules for searching like
using
import
differs
using
in that the new method of extended import must be used to import this
import
In
MyModule
to add a
show
the standard show feature, so we had to write down
import Base.show
Functions with function names that can only be seen through the
using
function cannot be extended.
importall
keyword explicitly imports all the names of the specified modules for
import
uses all the names in them alone.
Once a variable is made
using
import
a module may not be able to create its own variable with the same name.
Input variables must be read-only;
When we want to load a module, we can use two main keywords:
using
and
import
To understand their differences, consider the following example:
module MyModule
export x, y
x() = "x"
y() = "y"
p() = "p"
end
In this module we export the
export
and
y
x
the keyword export) and also include the non-export function
p
We have several different ways to load the module and its internal functionality into the current workspace, as follows:
Import commands | Import variables | Method to extend the available items |
---|---|---|
using MyModule | All export ed names (x and y), MyModule.x, MyModule.y and MyModule.p MyModule.x, MyModule.y and MyModule.p | |
using MyModule .x, MyModule.p | x and p | |
using MyModule: x, p | x and p | |
import MyModule | MyModule.x, MyModule.y and MyModule.p | MyModule.x, MyModule.y and MyModule.p |
import MyModule.x, MyModule.p | x and p | x and p |
import MyModule: x, p | x and p | x and p |
importall MyModule | All export ed names (x and y) | x and y |
In most cases, the file and file name are independent of the module; A module can have more than one file, and a file can have more than one module:
module Foo
include("file1.jl")
include("file2.jl")
end
Including the same code in different modules creates mixin-like characteristics. Y ou can take advantage of this to run the same code under different environment definitions, such as running a "safe" version of some operations for code testing:
module Normal
include("mycode.jl")
end
module Testing
include("safe_operators.jl")
include("mycode.jl")
end
There are three important standard modules: Main, Core, and Base.
Main is a top-level module, and Julia starts by setting Main to the current module.
In prompt mode, variables are defined in the Main module, and
whos()
all the variables in Main.
Core contains all of the "built-in" markers, such as some core languages, but does not include libraries.
Each module implicitly calls
using Core
nothing can be done without these declarations.
Base is a standard library (under base/folder).
All modules implicitly call
using Base
it is required in most cases.
With
using Base
the module explicitly introduces all operators.
The module also automatically
eval
definition of the eval function, which evaluates the expression in this module.
If you don't want these definitions,
baremodule
keyword to define modules.
When
baremodule
a standard module has the following format:
baremodule Mod
using Base
importall Base.Operators
eval(x) = Core.eval(Mod, x)
eval(m,x) = Core.eval(m, x)
...
end
Entering
using foo
Julia first looks
Main
in the
Foo
I
f the module is not found, Julia
require("Foo")
Typically, this loads the module from the installed package.
However, some modules also have sub-modules, which means that some
Main
sometimes be referenced directly from Main. T
here are two ways to solve this problem: Method One, using an absolute path, such
using Base.Sort
Method two, using a relative path, which makes it easy to load sub-modules of the current module or nested modules:
module Parent
module Utils
...
end
using .Utils
...
end
Module
Parent
sub-module
Utils
I
f you want
Utils
to be visible to
Parent
you
using
plus an English period. M
ore periods mean searching in a lower layer of namespaces. F
or example,
using ..Utils
look
Parent
within the sub-modules of the
Utils
The global variable
LOAD_PATH
the directory
require
Julia search module when the request is called.
You
push!
:
push!(LOAD_PATH, "/Path/To/My/Module/")
Putting this piece of code in
~\.juliarc.jl
extends to the LOAD_PATH
LOAD_PATH
You can also extend Julia's
JULIA_LOAD_PATH
defining the environment variables and information.
If a name is licensed (such as
Base.sin
can still be read externally even if it is not exported.
This is useful when debugging.
When you import or export a macro, add a sign before the
@
name, such
import Mod.@mac
Macros in other modules can be
Mod.@mac
@Mod.mac
The
M.x = y
is wrong and cannot assign a value to a global variable in another module;
Declare global x directly at
global x
and you can declare variables as reserved.
This can be used to prevent global variable initialization from encountering naming conflicts when loading.