Version 13 of comment

Updated 2006-07-13 14:11:30

If a hash character ("#") appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command. (From: TclHelp).


Tcl students frequently ask "Why can I not place unmatched braces in Tcl comments?"


Tcl comments are almost like null-operation commands. You could write one yourself like this:

 proc -- args {}
 -- This is a call to the new proc that does nothing, like a comment
 -- but this here still does an [exit]!! So it's not like
 # [exit] [exit] [exit] ... and still won't die ;-)

Note the "if 0 ..." idiom:

 if 0 {
 Any Tcl code to be commented out (with matching braces, of course!)
 or any other kind of text, will be ignored - and even [exit] won't fire
 because it's in braces, so left unevaluated!

} and a fancy sugar for that: "#" is special only if first character of a command name. Nobody hinders you to write a

 proc {#} args {}

where the braces are only visual markup - the parser strips them off, the proc's name will be just "#". But to call it, you have to escape the "#" sign - with a backslash, or with my favorite sugar:

 {#} {
   This is a comment in braces
 }

where the name of the command is pretty self-documenting: "comment in braces"... Much more intuitive than e.g. Ruby's "=begin ... =end", and available with almost zero implementation! (RS)

In the Tcl chatroom, MS pointed out: "the bytecode compiler recognizes "proc whatever args {}" and compiles no invocation for that; the cost is (almost) nothing. For other empty procs, an invocation will be compiled, so that you have a call/return overhead", and that "if 0 {...}" may raise errors on some contents (up to tcl8.3). Any word corresponding to a bcc'ed command at the start of a line is risky ...:

 % if 0 {set a b c}
 % if 0 {if you want to write this, you can't}
 % if 0 {while you read this, tcl errs out}

Therefore,

 {#} {set any number of words; if you so wish} 

is more robust than "if 0 {...}", as long as you DO NOT forget the braces, and before 8.4. But

 {#} do not [[exit]]

*will* exit ...


AB Forgive me for an easy question, but is there a way to comment out a whole block of code in TCL? For instance, I know in C++ you can do so by typing "/*", and then everything that follows is commented out until the compiler comes to "*/". Thanks!

Use

  if {0} {
     ... code to comment ...
  }

MG Tcl doesn't have -real- block-commenting like that, though. The if {0} { ... } method won't work if you have mismatched braces inside, or anything like that - the first mismatched } will end the if, and the code after it will start running.

(noname) 18-Aug-05 Try it! Use

  # First, define an block comment procedure, as:
  proc /* {args} {}
  # Then,you can use it as:
  /* { ...
  .. any comments,(with carefull { } pairs).
  } and more args in this line will be ignored.

RS 2006-07-13: In the Tcl chatroom someone asked how to have comments in lists. First answer is, lists are data, and comments apply only where commands are parsed; but the second answer is: of course! Lists in a source file come as strings (often in {} braces), and that Tcl is good at doing things with strings is well-known :^) One regsub is enough to strip substrings between # and the next newline:

 set data {
    # This is an example of comments in data
    test {
         4711 # an integer
         all  # a word
    }
    date today # one more test
 }
 % regsub -all {#.*?\n} $data \n


    test {
         4711 
         all  
    }
    date today 

RLH Wouldn't it be nifty:

 #-
   Everything in here is a comment or something similiar.
 #-

Tcl syntax help - Arts and crafts of Tcl-Tk programming