跳轉到內容

Lua 程式設計/註釋

來自華夏公益教科書,自由的教科書

註釋是程式中可以包含的文字或空白,用於使程式碼對程式設計師更易理解。註釋不會影響正在執行的程式,並且會被 lua 直譯器忽略。

註釋以雙連字元開頭

[編輯 | 編輯原始碼]

在 lua 中,註釋以雙連字元開頭,一直執行到行尾

-- This is a comment
print "Hello World!"

塊註釋

[編輯 | 編輯原始碼]

塊註釋以 doubleshovel 開頭,一直執行到 doubleclosebox

--[[Comments can be spread
across several lines ]]
print "Hello World!"

註釋中的啟用程式碼

[編輯 | 編輯原始碼]

透過將 hyphen 新增到 doubleshovel 中以形成 long handled doubleshovel,這使註釋中的程式碼能夠啟用,而不是被註釋掉。

---[[The long handled doubleshovel means that this code will run
print "This will print because it is not a comment!"
-- We can still include comments by prefixing them with a doubledash
-- print "This will not print because it is commented out"
]]

巢狀註釋

[編輯 | 編輯原始碼]

塊註釋的一個限制是,不可能在不從巢狀程式碼中剝離 doubleshovel 和 doubleclosebox 符號的情況下巢狀包含註釋的現有程式碼。

--[[
  This will not work. Nesting of comment blocks will cause a syntax error.
  --[[
    By nesting existing code containing a comment, a syntax error will occur. This is because the nested symbols will confuse the interpreter, so a modification is required to remove the symbols
  ]]
  print "Hello"
]]

程式碼需要修改如下

--[[
  The nested comment block markers have been removed
  @ This existing comment has had its block markers removed to prevent a syntax error
  @
  print "Hello"
]]
華夏公益教科書