Skip to main content

模組 (Modules)

模組是核心的程式單元,用於定義型別及操作這些型別的函式。結構型別定義 Move 儲存空間的結構描述,而模組函式則定義與這些型別值互動的規則。雖然模組本身也儲存在儲存空間中,但無法從 Move 程式內存取。在區塊鏈環境中,模組會儲存在鏈上,此程序通常稱為「發布」。發布後,可依照該特定 Move 實作的規則呼叫 entry 與 public 函式。

語法 (Syntax)​

模組具有下列語法:

module <address>::<identifier> {
(<use> | <type> | <function> | <constant>)*
}

其中,<address> 是有效的 地址,用於指定模組所屬的套件。

例如:

module 0::test;

use std::debug;

const ONE: u64 = 1;

public struct Example has copy, drop { i: u64 }

public fun print(x: u64) {
let sum = x + ONE;
let example = Example { i: sum };
debug::print(&sum)
}

名稱 (Names)​

module test_addr::test 部分指定模組 test 將會發布在套件設定中指派給名稱 test_addr 的數值地址下。

模組通常應使用具名地址宣告(而非直接使用數值)。例如:

module test_addr::test;

use std::debug;
use test_addr::another_test;

public struct Example has copy, drop { a: address }

public fun print() {
let example = Example { a: @test_addr };
debug::print(&example)
}

這些具名地址通常會與套件名稱相符。

由於具名地址僅存在於原始碼語言層級及編譯期間,因此在位元碼層級中,具名地址會完全替換為其值。例如,若我們有下列程式碼:

fun example() {
my_addr::m::foo(@my_addr);
}

且我們在 my_addr 設定為 0xC0FFEE 時編譯它,則其執行效果等同於下列程式碼:

fun example() {
0xC0FFEE::m::foo(@0xC0FFEE);
}

雖然在原始碼層級中,這兩種不同的存取方式等效,但最佳做法是一律使用具名地址,而非指派給該地址的數值。

模組名稱可由 a 至 z 的小寫字母或 A 至 Z 的大寫字母開頭。第一個字元之後,模組名稱可包含底線 _、a 至 z 的字母、A 至 Z 的字母,或 0 至 9 的數字。

module a::my_module {}
module a::foo_bar_42 {}

通常,模組名稱以小寫字母開頭。名為 my_module 的模組應儲存在名為 my_module.move 的原始碼檔案中。

成員 (Members)​

module 區塊中的所有成員可依任意順序出現。從根本上說,模組是由 types 與 functions 組成的集合。use 關鍵字會參考其他模組的成員。const 關鍵字會定義可在模組函式中使用的常數。

friend 語法是用於指定受信任模組清單的已淘汰概念。此概念已由 public(package) 取代。