Subject: Re: commenting in switch statements From: Bryan Oakley Date: 1997/04/03 Message-ID: <3343D495.237C228A@healthcare.com> Newsgroups: comp.lang.tcl lvirden@cas.org wrote: > > According to Flemming Madsen : > :In article , > : Aaron G Goldstein writes: > :> I've discovered an interesting behaviour in the way the switch command > :> works in tcl 7.6. > :> > :> The following three switch statements will all behave differently: > :> > [examples removed] > :. > :> Any opinions? > :> > : > :From a Tcl man page: > : > : If the first non-blank character in a command is #, then > : everything from the # up through the next newline character > : is treated as a comment and ignored. > : > :This implies that you can only put comments in a place, where tcl > :expects a command eg: > > Okay, but how does this explain why a varying comment line causes 3 > different behaviors in the script? I will grit my teeth and attempt > to stand this behavior (which causes the folk for whom tcl was intended > no small amount of grief). But, I think that it is not unfair to expect > in return that Tcl behave consistently. And what, exactly, is inconsistent with this behavior? Wait! Don't answer that; I will. What is inconsistent is that the switch statement and comment processing is different for tcl than for many other languages. The switch statement and comment processing within tcl is 100% consistent with the rest of tcl. One has to remember that there is no such thing as a comment in tcl. There is a comment _command_, but it's not really a comment. Just a no-op command. As such, it can only be used where commands can be used. In this case one should also remember that the switch statement simply takes a list of patterns and bodies. There must be a body for each pattern, and the body must be executable. When one does something like this: switch -exact -- foo { bar {puts bars} # three word comment default {puts lose} } The "#" is a pattern, and "three" is the body for that pattern. It may *look* like a comment, but it's not. It's really no different that something like set foo "some string with a # sign in it" Now, you don't really expect '# sign in it'" to be treated as a comment do you? Of course not. The simple argument is "because it's inside quotes". Well, '# three word comment' in the above switch statement is inside quotes too. Only the quote character is a curly brace instead of a double quote mark and thus #, three, word and comment are all treated as members of a list. (wow, talk about quoting hell... more single quotes, double quotes and the word "quote" than squashed armadillos on a texas state highway :-) The problem is, while tcl is (arguably) syntacticly pure, humans are not. Even though we know in our hearts that # is just a command, we wish it to be otherwise. We *think* it is a real comment and thus expect it to behave as one; only, it doesn't. -- Bryan Oakley mailto:oakley@healthcare.com Software Engineer http://www1.clearlight.com/~oakley/ Healthcare Communications, Inc. http://www.healthcare.com/