Line, block and nested comments - Lua compared to other languages
Archive - Originally posted on "The Horse's Mouth" - 2014-05-04 18:12:58 - Graham EllisYou can comment your Lua program in two ways.
a) using comments that start -- , in which case they run to the end of the line - so that's a line comment
b) starting --[[ , in which case they run through to the next ]] which may be in the same line, or a number of lines later - so that's a block comment
Line and block comments are common in many other languages too
• in PHP, for example, a line comment starts with a # or //, and a block comment runs from /* to */.
• in C++ and in Java, line comments start with //, and block comments run from /* to */.
• ANSI C only supports block comments from /* to */, though some extended compilers accept line comments from //.
&bull, In Tcl, Perl and Python you have just line comment support from # to the end of the line
and in all the languages we teach, blank lines will be ignored too so are in effect a separating comment
One of the things I regret about block comments in most langauges is that they cannot be nested. In other words, you can't write a comment with a comment. "Does this matter?" you ask. Yes, it does to me ... I want to be able to comment out a whole block of code that's already got comments within it, and that's a problem where the end of the inner comment also forces the end of the outer comment. Lua (uniquely in the languages we teach) provides a solution in that block comments can start with extra = signs between the [ and [, and then the terminating sequeence need to have the same number of == signs ... so
print ("This will be printed first")
--[===[ Comment out a whole big block!
--[[ This is a multiline comment telling you about
the work this code is doing ]]
print ("This will not be printed in the middle")
--[[ This is another multiline comment telling you about
the work this code is doing ]]
end of big block comment ]===]
print ("This will be printed last")
will just print out two lines, with the one in the middle being skipped.
Examples, in context - see [here].