註解 (Comments)
註解是一種為程式碼加上筆記或說明文件的方式。編譯器會忽略它們,不會產生任何 Move bytecode。你可以用註解來解釋程式碼的作用、給自己或其他開發者留下筆記、暫時移除一部分程式碼,或是產生文件。Move 中有三種註解:行註解、區塊註解,以及文件註解。
行註解 (Line Comment)
你可以用雙斜線 // 來註解掉該行剩下的內容。// 之後的所有內容都會被編譯器忽略。
module book::comments_line;
// let's add a note to everything!
fun some_function_with_numbers() {
let a = 10u8;
// let b = 10 this line is commented and won't be executed
let b = 5; // here comment is placed after code
a + b; // result is 15, not 10!
}
區塊註解 (Block Comment)
區塊註解用來註解掉一整段程式碼。它們以 /* 開頭,以 */ 結尾。/* 和 */ 之間的所有內容都會被編譯器忽略。你可以用區塊註解來註解掉單行或多行程式碼,甚至可以用它們來註解掉一行中的一部分。
module book::comments_block;
fun /* you can comment everywhere */ go_wild() {
/* here
there
everywhere */ let a = 10;
let b = /* even here */ 10; /* and again */
a + b;
}
/* you can use it to remove certain expressions or definitions
fun empty_commented_out() {
}
*/
這個範例有點極端,但它展示了所有可以使用區塊註解的方式。
文件註解 (Doc Comment)
文件註解是用來為程式碼產生文件的特殊註解。它們與行註解類似,但以三個斜線 /// 開頭,並放置在其所說明項目——模組、結構、函式或常數——的定義之前。
/// Module has documentation!
module book::comments_doc;
/// This is a 0x0 address constant!
const AN_ADDRESS: address = @0x0;
/// This is a struct!
public struct AStruct {
/// This is a field of a struct!
a_field: u8,
}
/// This function does something!
/// And it's documented!
fun do_something() {}
文件工具會將公開成員的文件註解收集到參考頁面中——貫穿本書所連結的 標準函式庫與框架文件正是以這種方式產生的。一個寫得好的文件註解會說明函式的作用,以及在什麼條件下會中止。
空白字元 (Whitespace)
與某些語言不同,空白字元(空格、tab 和換行)對程式的意義沒有任何影響。