Importing Modules
Move achieves high modularity and code reuse by allowing module imports. Modules within the same package can import each other, and a new package can depend on already existing packages and use their modules too. This section will cover the basics of importing modules and how to use them in your own code.
Importing a Module
Modules defined in the same package can import each other. The use
keyword is followed by the module path, which consists of the package address (or alias) and the module name separated by ::
.
Another module defined in the same package can import the first module using the use
keyword.
Importing Members
You can also import specific members from a module. This is useful when you only need a single function or a single type from a module. The syntax is the same as for importing a module, but you add the member name after the module path.
Grouping Imports
Imports can be grouped into a single use
statement using the curly braces {}
. This is useful when you need to import multiple members from the same module. Move allows grouping imports from the same module and from the same package.
Single function imports are less common in Move, since the function names can overlap and cause confusion. A recommended practice is to import the entire module and use the module path to access the function. Types have unique names and should be imported individually.
To import members and the module itself in the group import, you can use the Self
keyword. The Self
keyword refers to the module itself and can be used to import the module and its members.
Resolving Name Conflicts
When importing multiple members from different modules, it is possible to have name conflicts. For example, if you import two modules that both have a function with the same name, you will need to use the module path to access the function. It is also possible to have modules with the same name in different packages. To resolve the conflict and avoid ambiguity, Move offers the as
keyword to rename the imported member.
Adding an External Dependency
Every new package generated via the sui
binary features a Move.toml
file with a single dependency on the Umi Network Framework package. The Umi Network Framework depends on the Standard Library package. And both of these packages are available in default configuration. Package dependencies are defined in the Package Manifest as follows:
The dependencies
section contains a list of package dependencies. The key is the name of the package, and the value is either a git import table or a local path. The git import contains the URL of the package, the subdirectory where the package is located, and the revision of the package. The local path is a relative path to the package directory.
If a dependency is added to the Move.toml
file, the compiler will automatically fetch (and later refetch) the dependencies when building the package.
Importing a Module from Another Package
Normally, packages define their addresses in the [addresses]
section, so you can use the alias instead of the address. For example, instead of 0x2::coin
module, you would use sui::coin
. The sui
alias is defined in the Umi Network Framework package. Similarly, the std
alias is defined in the Standard Library package and can be used to access the standard library modules.
To import a module from another package, you use the use
keyword followed by the module path. The module path consists of the package address (or alias) and the module name separated by ::
.