Constants 常數 (Constants)
常數是在模組層級定義的不可變值。它們通常用來為整個模組中使用的靜態值命名。例如,如果某個產品有預設價格,你可能會為它定義一個常數。常數儲存在模組的位元組碼中,每次使用時都會複製該值。跟每個模組成員一樣,常數預設是私有的——而且與函式或結構體不同,常數無法被設為公開;下方的 config 模式 展示了如何在模組之間共享它們。
module book::shop_price;
use sui::{coin::Coin, sui::SUI};
/// Trying to purchase an item at an incorrect price.
const EWrongPrice: u64 = 0;
/// The price of an item in the shop.
const ITEM_PRICE: u64 = 100;
/// The owner of the shop, an address.
const SHOP_OWNER: address = @0xa11ce;
/// An item sold in the shop.
public struct Item {}
/// Purchase an item from the shop.
public fun purchase(coin: Coin<SUI>): Item {
assert!(coin.value() == ITEM_PRICE, EWrongPrice);
transfer::public_transfer(coin, SHOP_OWNER);
Item {}
}
命名慣例 (Naming Convention)
常數必須以大寫字母開頭——這是在編譯器層級強制執行的。對於作為值使用的常數,慣例是使用全大寫字母並在單字之間加底線,這讓常數在程式碼中與其他識別字有所區別。錯誤常數是個例外,它們寫成 E 後接 CamelCase 描述,例如 ENoAccess。
/// Price of the item used at the shop.
const ITEM_PRICE: u64 = 100;
/// Error constant.
const EItemNotFound: u64 = 1;
常數是不可變的 (Constants Are Immutable)
常數不能被更改或指派新值。作為套件位元組碼的一部分,它們本質上是不可變的。
module book::immutable_constants;
const ITEM_PRICE: u64 = 100;
// 會發出錯誤
fun change_price() {
ITEM_PRICE = 200;
}
使用 Config 模式 (Using the Config Pattern)
應用程式的常見用例是定義一組在整個程式碼庫中使用的常數。但由於常數是模組私有的,其他模組無法存取它們。解決這個問題的一種方式是定義一個「config」模組,透過公開函式匯出這些常數:
module book::config;
const ITEM_PRICE: u64 = 100;
const TAX_RATE: u64 = 10;
const SHIPPING_COST: u64 = 5;
/// Returns the price of an item.
public fun item_price(): u64 { ITEM_PRICE }
/// Returns the tax rate.
public fun tax_rate(): u64 { TAX_RATE }
/// Returns the shipping cost.
public fun shipping_cost(): u64 { SHIPPING_COST }
如此一來,其他模組就可以匯入並讀取這些常數,同時簡化了更新流程。如果需要變更常數,只需要在套件升級時更新 config 模組即可。
延伸閱讀 (Further Reading)
- Move Reference 中的常數
- 常數的程式碼撰寫慣例