Skip to main content

匯入模組 (Importing Modules)

Move 透過允許匯入模組,實現高度模組化與原始碼重用。同一個套件內的模組可以彼此匯入,而新的套件也可以依賴既有套件並使用其中的模組。本節將介紹匯入模組的基礎知識,以及如何在你自己的原始碼中使用它們。

匯入模組 (Importing a Module)​

在同一個套件中定義的模組可以彼此匯入。use 關鍵字後方接著模組路徑,該路徑由套件地址(或別名)與模組名稱組成,兩者以 :: 分隔。

File: sources/module_one.move
module book::module_one;

/// Struct defined in the same module.
public struct Character has drop {}

/// Simple function that creates a new `Character` instance.
public fun new(): Character { Character {} }

同一套件中定義的另一個模組可以使用 use 關鍵字匯入第一個模組。

File: sources/module_two.move
module book::module_two;

use book::module_one; // importing module_one from the same package

/// Calls the `new` function from the `module_one` module.
public fun create_and_ignore() {
let _ = module_one::new();
}

注意:任何你想從另一個模組匯入的項目(結構、函式、常數等),都必須以 public(或 public(package),請參閱可見性修飾詞)關鍵字標記,才能在其定義模組外部存取。例如,module_one 中的 Character 結構與 new 函式皆已標記為公開,因此可在 module_two 中使用。

匯入成員 (Importing Members)​

你也可以從模組匯入特定成員。當你只需要模組中的單一函式或單一型別時,這會很有用。其語法與匯入模組相同,但需在模組路徑後方加上成員名稱。

module book::more_imports;

use book::module_one::new; // imports the `new` function from the `module_one` module
use book::module_one::Character; // importing the `Character` struct from the `module_one` module

/// Calls the `new` function from the `module_one` module.
public fun create_character(): Character {
new()
}

將匯入分組 (Grouping Imports)​

你可以使用大括號 {},將匯入項目分組至單一 use 陳述式中。從同一個模組或套件匯入多個成員時,這能讓原始碼更整潔且更有條理。

module book::grouped_imports;

// imports the `new` function and the `Character` struct from
// the `module_one` module
use book::module_one::{new, Character};

/// Calls the `new` function from the `module_one` module.
public fun create_character(): Character {
new()
}

在 Move 中,匯入函式名稱較不常見,因為函式名稱可能重複而造成混淆。建議的做法是匯入整個模組,並使用模組路徑存取函式。型別具有唯一名稱,應個別匯入。

若要在一次群組匯入中同時匯入模組本身與其部分成員,請使用代表該模組的 Self 關鍵字:

module book::self_imports;

// imports the `Character` struct, and the `module_one` module
use book::module_one::{Self, Character};

/// Calls the `new` function from the `module_one` module.
public fun create_character(): Character {
module_one::new()
}

解決名稱衝突 (Resolving Name Conflicts)​

從不同模組匯入多個成員時,可能會發生名稱衝突。例如,若你匯入兩個都具有相同名稱函式的模組,便需要使用模組路徑存取該函式。不同套件中也可能存在相同名稱的模組。為解決衝突並避免歧義,Move 提供 as 關鍵字來重新命名匯入的成員。

module book::conflict_resolution;

// `as` can be placed after any import, including group imports
use book::module_one::{Self as mod, Character as Char};

/// Calls the `new` function from the `module_one` module.
public fun create(): Char {
mod::new()
}

新增外部依賴項 (Adding an External Dependency)​

Move 套件可以依賴其他套件;依賴項會列於名為 Move.toml 的套件清單文件中。

套件依賴項在套件清單中的定義如下:

Move.toml
[dependencies]
Example = { git = "https://github.com/Example/example.git", subdir = "path/to/package", rev = "v1.2.3" }
Local = { local = "../my_other_package" }

dependencies 區段包含每個套件依賴項的一個項目。項目的鍵是套件名稱(範例中的 Example 或 Local),而值則是 git 匯入表格或區域路徑。git 匯入包含套件 URL、套件所在的子目錄,以及套件修訂版本。區域路徑則是套件目錄的相對路徑。

編譯器會在建置套件時自動擷取(並在後續重新擷取)列出的依賴項,而其所有依賴項也會提供給你的套件使用。

自 sui CLI 1.45 版起,若 Move.toml 中未包含系統套件,系統套件便會自動納入為所有套件的依賴項。因此,無須明確匯入即可使用 MoveStdlib、Sui、System、Bridge 與 Deepbook。

從另一個套件匯入模組 (Importing a Module from Another Package)​

一般而言,套件會在 [addresses] 區段中定義其地址。你可以使用別名來取代完整地址。例如,與其使用 0x2::coin 參考 Sui 的 coin 模組,你可以使用 sui::coin。sui 別名定義於 Sui Framework 套件的套件清單中。同樣地,std 別名定義於 Standard Library 套件中,可用來取代 0x1 以存取標準函式庫模組。

若要從另一個套件匯入模組,請使用 use 關鍵字後接模組路徑。模組路徑由套件地址(或別名)與模組名稱組成,兩者以 :: 分隔。

module book::imports;

use std::string; // std = 0x1, string is a module in the standard library
use sui::coin; // sui = 0x2, coin is a module in the Sui Framework

注意:模組地址名稱取自套件清單文件(Move.toml)的 [addresses] 區段,而非 [dependencies] 區段中使用的名稱。

延伸閱讀 (Further Reading)​