第一天学习Lua,正在按照《Lua程序设计(中文版)》学习。运行2.4节“使用[[...]]表示字符串”出现“nesting of [[...]] is deprecated near '['”的错误,错误截图如下。那么要如何解决呢?
查阅Programming in Lua(3rd ed)P32,可以找到答案。
Sometimes, you may want to enclose a piece of code containing somethinglike a=b[c[i]] (notice the ]] in this code), or you may need to enclose some codethat already has some code commented out. To handle such cases, you can addany number of equal signs between the two open brackets, as in [===[. Afterthis change, the literal string ends only at the next closing brackets with thesame number of equal signs in between (]===], in our example). The scannerignores pairs of brackets with a different number of equal signs. By choosing anappropriate number of signs, you can enclose any literal string without havingto add escapes into it.
由上所述,当使用“[[…]]]”的方式标记字符串时,如果这段字符串中出现了“]]”,为防止错误,可以改用“[==[…]==]”来包围字符串。其中,等号的数量没有特殊要求,只要前后双中括号中所夹等号的数量保持一致,就被视为是对应的一对。比如用“[=[”和“]=]”以及“[===[”和“]===]”包围字符串,都是可行的。同理,当我们用“--[[…--]]”进行多行注释时,也可以通过添加等号的方式规避类似错误。