Version 5 of Comments inside Exec

Updated 2003-04-03 14:31:28

A short question in comp.lang.tcl got me several useful answers that I'm now putting here for later reference:

The (generalized) question:

  • How do I embed comments into an expr (or the first argument to if,while)

The specific context:

  • an expression was structured as an "&&"ed sequence of separate conditions, each of which should be associated with a simple text (e.g. someone's name).
  • It was considered a valid assumption, that the comment text does not contain "nasty characters".
  • Readability weighs more than performance.

The answers:

by Roy Terry:

  • use a proc that returns first argument and ignores the rest. (slightly modified from posting.)
   # proc ? {cond args} {return $cond}
   # RT - this version ought to work more flexibly
   proc ? {cond args} { return [uplevel expr $cond]}
   if { [? {<cond1>}  John Doe ]
       && [? {<cond2>}  Fu   Bar ]
       ...
   } ...

This works nicely, if the cond's are plain values, rather than expressions (which I forgot to specify in my posting), however it can still be used to wrap the specific value inside each condition. See 2nd version of proc "?" with expr above - RT.

  • use Package "Tmac", which allows embedding comments everywhere: [L1 ]
   if { cond1 (* John Doe *)
      && cond2 (*  Fu   Bar *)
      ...
   } ...

by Ulrich Schoebel

  • build up the expression condition by condition:
   set cond [list]
   lappend cond $cond1 ;# John Doe                                                 
   lappend cond $cond2 ;# Fu Bar                                                   
   ...
   if {[expr [join $cond &&]]} { ...                                                                             

Thanks for all the hints! Andreas Leitgeb