Subject: Re: commenting in switch statements From: Bryan Oakley Date: 1997/04/05 Message-ID: <33467E7B.ABD322C@healthcare.com> Newsgroups: comp.lang.tcl lvirden@cas.org wrote: > > According to Nick Reingold : > :Tcl IS behaving consistently. In the three cases you refer to, the # > :did NOT start a comment (because the # was where a *pattern* was > :expected). > > Okay, I am sorry for being dense here. But if someone would have > the patience to explain (feel free to use email if necessary) the > above statement that's been made several times in this thread I would > love to hear how getting 3 different results is 'consistent behavior'. > > I understand about # being a command. I've been around this newsgroup > since the beginning of it, and see this problem at least every month - > sometimes every day. I provide a limited amount of support for tcl > for a large group of non-programmers who have to use tcl daily. > > The inconsistency I speak of is between the last two cases. Why > is Tcl behaving consistently to generate one error when the # pattern > has 4 arguments than when it has 3? Short answer: because the # pattern has one argument; after that are more patterns and actions. It only *looks* like all that belongs to the # pattern. I don't think you can see the trees for the forest (the last switch argument is the forest, the list elements are the trees...) The last argument to the switch statement is a list. This list must have an even number of patter/action elements. It's in the man page, but you do have to infer it -- it doesn't come right out and say you must have an even number of elements. Whitespace will make this easier to explain. Here's the first case: # example A1 switch -exact -- foo { bar {puts bar} # four word comment line default {puts lose} } ... which is exactly the same as this: # example A2 switch -exact -- foo { bar {puts bar} # four word comment line default {puts lose} } Remember, the last argument to switch is a list of elements, and there must be an even number of them. Notice that because of the 'comment', we are short one action. Hence the syntax error. Next case: # example B1 switch -exact -- foo { bar {puts bar} # three word comment default {puts lose} } ..is the same as this: # example B2 switch -exact -- foo { bar {puts bar} # three word comment default {puts lose} Now we have an even number of list elements, so no syntax error. To futher hammer this thing home, here is a quick test using a slight variation of B1: proc three {} { puts "'\#' was matched" } proc comment {} { puts "'word' was matched." } foreach foo [list bar \# word bogus] { switch -exact -- $foo { bar {puts bar} # three word comment default {puts lose} } } ----------------- $ tclsh7.6 bar '#' was matched 'word' was matched. lose Notice that '#' is treated as a pattern, and calls 'three' when it is matched. Also, 'word' is a pattern with 'comment' being it's command. Does that make the consistency more apparent? It's consistent because the # character is not a comment in this case, just an element in a list passed to the switch statement. If that 'comment' has an odd number of words, it's going to bust the switch statement (if the switch is otherwise correct...). Perhaps we should treat all this as a bug in the man page. If the man page was more explicit about how the list of elements is processed, would that help? -- Bryan Oakley mailto:oakley@healthcare.com Software Engineer http://www1.clearlight.com/~oakley/ Healthcare Communications, Inc. http://www.healthcare.com/