Module imports
For information about creating a module, see Modules.
Modules can be imported using the import
keyword:
This program can use any public definitions from the os
module, such
as the input
function. See the standard library
documentation for a list of common modules and their public symbols.
By default, you have to specify the module prefix every time you call an external function. This may seem verbose at first, but it makes code much more readable and easier to understand - it's always clear which function from which module is being called. This is especially useful in large code bases.
Cyclic module imports are not allowed, like in Go.
Selective imports
You can also import specific functions and types from modules directly:
[!NOTE] This will import the module as well. Also, this is not allowed for constants - they must always be prefixed.
You can import several specific symbols at once:
Module hierarchy
[!NOTE] This section is valid when .v files are not in the project's root directory.
Modules names in .v files, must match the name of their directory.
A .v file ./abc/source.v
must start with module abc
. All .v files in this directory
belong to the same module abc
. They should also start with module abc
.
If you have abc/def/
, and .v files in both folders, you can import abc
, but you will have
to import abc.def
too, to get to the symbols in the subfolder. It is independent.
In module name
statement, name never repeats directory's hierarchy, but only its directory.
So in abc/def/source.v
the first line will be module def
, and not module abc.def
.
import module_name
statements must respect file hierarchy, you cannot import def
, only
abc.def
Refering to a module symbol such as a function or const, only needs module name as prefix:
can be called like this:
A function, located in abc/def/source.v
, is called with def.func()
, not abc.def.func()
This always implies a single prefix, whatever sub-module depth. This behavior flattens modules/sub-modules hierarchy. Should you have two modules with the same name in different directories, then you should use Module import aliasing (see below).
Module import aliasing
Any imported module name can be aliased using the as
keyword:
[!NOTE] This example will not compile unless you have created
mymod/sha256/somename.v
(submodule names are determined by their path, not by the names of the .v file(s) in them).
You cannot alias an imported function or type. However, you can redeclare a type.