Sui Move Language - Imports, aliases, Constants

Sui Move Language - Imports, aliases, Constants


2 min read

Hello everyone..

Welcome to another day of exploring Web3 Engineering. In this series, we are learning the Move language for smart contract development on Sui blockchain. In this article, let understand on how to import another packages into our current module. So, without any further ado, let us get started.

Imports - use keyword

To import any module into or a function or a package into our current module, move provides use keyword for that. Here is the syntax of the use statement.

use published_address::module_name;
use published_address::module_name::function_name;

// Example
use std::object; // std is the named address for @0x01
use sui::coin::transfer;

We can also import multiple required functions from a package in a simple statement using curly braces {}. The syntax is as follows:

user publised_address::module_name::{fun1, fun2, ...};

// Example
use sui::tx_context::{sender, epoch};

Aliases - as keyword

To resolve name conflicts between the module imports, we can created aliases for the imported members with the help of as keyword. The syntax for the as keyword is as follows:

use published_address::module_name as alias_name;

// Example
use package_a::coin as c;

We can also alias the member imports too.

user package_a::coin::{Self as mod, trasnfer as custom_transfer};

Method alias

For modules that define multiple structs and their methods, it is possible to define method aliases to avoid name conflicts, or to provide a better-named method for a struct.

The syntax for aliases is:

// for local method association
use fun function_path as Type.method_name;

// exported alias
public use fun function_path as Type.method_name;

Constants - const keyword

To declare a constant on move contracts, we use const keyword. The naming convention for the constants is that the name should be all capital letters.

The syntax for constants is

const NAME: data_type = value;

// Example
const CONSTANT1: u8 = 1;
const OWNER_ADDRESS: address = @0xCAFE;

It is must to declare the type of the const as it can't be inferred from the value assigned to it.

In the upcoming, let us looking into the packages and then after let us start writing our first move smart contract.