commenting in switch statements 
Author Message
 commenting in switch statements

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:

switch -exact -- foo {
        bar {puts bars}
        # three word comment
        default {puts lose}

Quote:
}

...and...

switch -exact -- foo {
        bar {puts bars}
        # four word comment line
        default {puts lose}

Quote:
}

...and...

switch -exact -- foo {
        bar {puts bar}
        # this foo junk
        default {puts lose}

Quote:
}

The first will print the string "lose", as one would expect.

The second, fails, with the error:
        extra switch pattern with no body

The third also fails, and says:
        invalid command name "junk"

I don't know about anyone else, but this strikes me as being incorrect, or
at least silly.

After poking through the source, I found that Tcl doesn't strip comments
from the body of a switch statement, which is what causes this anomalous
behaviour.

Any opinions?

 _____________________________________________________________________
|  My opinions may have changed, but  |  When someone says "I want a  |
| the fact that I am correct has not. | programming language in which |
|         - random cool quote         |  I need only say what I want  |
|=====================================|  done," give him a lollipop.  |
|    "Share and Enjoy"  "jabanero"    |===============================|

|_____________________________________________________________________|



Sun, 19 Sep 1999 03:00:00 GMT  
 commenting in switch statements



Quote:
>I've discovered an interesting behaviour in the way the switch command
>works in tcl 7.6 [for commenting].

You have discovered basic rule #12 of Tcl: don't put comments inside
switch statements.  Don't feel bad, Dr. John actually slipped up on
this one in the release version of Tk4.2 (fixed in p2).

--

  Software Engineer, Oregon R&D          office: 541.683.7891
  CADIX International, Inc.              fax:    541.683.8325
             URL: http://www.cs.uoregon.edu/~jhobbs/



Sun, 19 Sep 1999 03:00:00 GMT  
 commenting in switch statements



Quote:
> 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:

.
.

Quote:
> 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:

puts var val   ;# assign val to var

/Flemming



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements




:> 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:
:>
:.
:.
:> 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.
--

<URL:http://www.teraform.com/%7Elvirden/> <*> O- "We are all Kosh."
Unless explicitly stated to the contrary, nothing in this posting should
be construed as representing my employer's opinions.



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements


: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.  -- Larry

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).

I happen to think the rule for # is brain-dead, but at least it's
applied consistently.

            -- Nick Reingold



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements

Quote:


> :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.  -- Larry

> 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).

> I happen to think the rule for # is brain-dead, but at least it's
> applied consistently.

>             -- Nick Reingold

Is this brain-dead behaviour preserved in 8.0? That is, does 8.0
continue to treat # as a command, or does the compiler have a lexical
phase that throws away comments in the usual way? I can not believe that
anyone WANTS # to be a command!!

--

VP Software Engineering    412 621 8333
NeoVision Hypersystems     412 621 8337 (fax)
5001 Baum Blvd
Pittsburgh, PA 15213



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements

Quote:




> :> 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.

--

Software Engineer                http://www1.clearlight.com/~oakley/
Healthcare Communications, Inc.  http://www.healthcare.com/



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements

Quote:

> Is this brain-dead behaviour preserved in 8.0? That is, does 8.0
> continue to treat # as a command, or does the compiler have a lexical
> phase that throws away comments in the usual way? I can not believe that
> anyone WANTS # to be a command!!

I do. Or at least, I'm perfectly happy with it being a command. One of
the most wonderful things about tcl is how few parsing rules there are
to remember. Sure, adding one more rule for comments wouldn't be that
bad, but it's also not necessary. Tcl is absolutely rock-solid in how it
interprets things which makes it quite easy to figure out what a line of
code will do.

--

Software Engineer                http://www1.clearlight.com/~oakley/
Healthcare Communications, Inc.  http://www.healthcare.com/



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements


|> >

|> > :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.  -- Larry
|> >
|> > 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).
|> >
|> > I happen to think the rule for # is brain-dead, but at least it's
|> > applied consistently.
|> >
|> >             -- Nick Reingold
|>
|> Is this brain-dead behaviour preserved in 8.0? That is, does 8.0
|> continue to treat # as a command, or does the compiler have a lexical
|> phase that throws away comments in the usual way? I can not believe that
|> anyone WANTS # to be a command!!

The problem is that this will be an incompatible change, and we're trying
as hard as we can to keep Tcl 8.0 backwards compatible, even if doing so
preserves warts like the current commenting mechanism.  For example,
if there are switch commands that actually use the string "#" as the
switch pattern, they would break if we start treating "#" as a comment in
that situation.



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements

Quote:

> The problem is that this will be an incompatible change, and we're trying
> as hard as we can to keep Tcl 8.0 backwards compatible, even if doing so
> preserves warts like the current commenting mechanism.  For example,
> if there are switch commands that actually use the string "#" as the
> switch pattern, they would break if we start treating "#" as a comment in
> that situation.

Any chance you could introduct a *new* commenting mechanism to go along

way we could all migrate to the new mechanism at our leisure.

FWIW: I really applaud your attempt at backwards compatibility. I think
it's crucial to get acceptance of 8.0. I know we'll be very hesitant to
move our product over, but the fewer incompatibilities there are, the
more likely we are to upgrade. Incompatibilities can come at 8.1 or 8.2,
but I think it's important to get as many people migrated to 8.0 as soon
as possible.

--

Software Engineer                http://www1.clearlight.com/~oakley/
Healthcare Communications, Inc.  http://www.healthcare.com/



Mon, 20 Sep 1999 03:00:00 GMT  
 commenting in switch statements


                        .
                        .
                        .

Quote:
>preserves warts like the current commenting mechanism.  For example,
>if there are switch commands that actually use the string "#" as the
>switch pattern, they would break if we start treating "#" as a comment in
>that situation.

There are such switches.  In real, live code.
Most who care probably already know this, but
perhaps there's value in making it explicit.
--

Cameron Laird           http://starbase.neosoft.com/~claird/home.html

                        +1 713 996 8546 FAX



Tue, 21 Sep 1999 03:00:00 GMT  
 commenting in switch statements



   >preserves warts like the current commenting mechanism.  For example,
   >if there are switch commands that actually use the string "#" as the
   >switch pattern, they would break if we start treating "#" as a comment in
   >that situation.

   There are such switches.  In real, live code.

Making "#" be handled specially would not only break real live switches
but it would break similar constructions.  Consider:

        expect {
           pattern1 action1
           # foo
           pattern2 action2
        }

In this example "#" is a pattern to be matched!  So if you contemplate
"fixing" Tcl, you potentially break any proc that anyone has ever
written.  Some people would argue, of course, that this is a good
thing but the fact is that there's no way for Tcl to know whether
anything in a string is a comment or not.  Consider this:

        set x {
           pattern1 action1
           # foo
           pattern2 action2
        }

Whether "# foo" is a comment or literal data depends upon later use of $x.
Selecting different characters (//) doesn't help at all.

Don



Tue, 21 Sep 1999 03:00:00 GMT  
 
 [ 29 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Switch statement with nested If statement

2. Switching from PHP to Ruby - Comments Please

3. comment sign (#) in switch body

4. Comments in switch

5. Comments on switching to the Cygnus development system

6. switch doesn't like comment (Tcl-Bug?)

7. control statements: nothing similar to SWITCH()?

8. non-ordered 'switch' statement

9. Eiffel approach to switch statements

10. multi-dimensional switch statements

11. Switch statement in (A)Rexx

12. making VRML objects disappear (also SWITCH statement)

 

 
Powered by phpBB® Forum Software