Modules, Imports & Visibility
A module groups types, functions, and constants under an address.
13.1 Module structure
A module groups types, functions, and constants under an address. It is the unit of encapsulation: only code inside the module can construct, destructure, or read fields of its structs.
// Sui Move 2024 — module label form
module my_pkg::bank;
use std::string::String;
use sui::balance::{Self, Balance};
use sui::sui::SUI;
public struct Bank has key { id: UID, reserve: Balance<SUI> }
const EZero: u64 = 0;
fun init(ctx: &mut TxContext) { /* runs once at publish */ }
public fun deposit(/* ... */) { /* ... */ }// Aptos Move — block form (also valid on Sui)
module my_addr::bank {
use std::signer;
use aptos_framework::coin::{Self, Coin};
use aptos_framework::aptos_coin::AptosCoin;
struct Bank has key { reserve: Coin<AptosCoin> }
// ...
}13.2 use forms
use declarations can also appear inside function bodies, scoped to that block.
use std::vector; // refer to vector::push_back
use std::string::String; // import a type
use std::string::{Self, String}; // module alias + type
use sui::coin::{Self as c, Coin}; // rename
use std::option::{Option, some, none}; // import functions13.3 Implicit imports
Sui Move 2024 implicit std::vector, std::option::{Self, Option}, sui::object::{Self, ID, UID}, sui::transfer, sui::tx_context::{Self, TxContext}
std::vector and primitive builtins only
13.4 Friends and package visibility
Aptos / classic Move — a module declares friends, which may call its public(friend) functions:
Move 2 also offers package visibility (public(package), shorthand package fun) which allows any module in the same package, and friend fun as shorthand for public(friend) fun.
Sui Move 2024 — friend is deprecated; use public(package):
module my_addr::core {
friend my_addr::router;
public(friend) fun internal_mint(/* ... */) { }
}public(package) fun internal_mint(/* ... */) { }13.5 Scripts (Aptos/Diem only)
Aptos supports compiled scripts for one-off multi-call transactions (aptos move compile-script, aptos move run-script). Sui replaces scripts with programmable transaction blocks (Chapter 35).
script {
use my_addr::counter;
fun main(account: &signer) {
counter::increment(account);
counter::increment(account);
}
}