Weird behavior of tcl comment inside if/else block. Is it a bug of tcl interpreter? -


i caught strange bug(?) took me whole day find in real application. in code there elseif block commented out , led execution of code (as thought) not ever executed.

i simplified testcase reproduces odd tcl behavior.

proc funnyproc {value} {   if {$value} {     return "true" #  } elseif {[puts "comment :)"] == ""} { #    return "comment"   } else {     return "false"   }   return "it's impossible!!!" }  puts [funnyproc false] 

what think program output?

  1. the puts in comment line executed. it's impossible programming language pov.
  2. the line after block if {...} {return} else {return} executed well. it's impossible true/false logic.

i know tcl-comment behaves command name # , consumes arguments until eol. , tcl parser not unbalanced curly brackets in comments. case out of understanding.

maybe missed important? how correctly comment out such elseif blocks, not have these weird side-effects?

this because # comment when tcl looking start of command, , first time sees above (when parsing if), it's looking } in order close earlier {. consequence of tcl parsing rules; if command, not special construct.

the effect ernest noted because increases nesting level of braces on line, makes part of argument runs end of if {$value} { line start of } else { line. # becomes special when if evaluates script. (well, except it's bytecode compiled, that's implementation detail: observed semantics same except nasty edge cases.)


Comments