C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Willem

    #31
    Re: C/C++ language proposal: Change the 'case expression' from "integr al constant-expression&quot ; to "integr al expression&quot ;

    Keith Thompson wrote:
    ) Then programmers will inevitably write
    )
    ) case 'A' ... 'Z':
    )
    ) which is non-portable (under EBCDIC it matches '\' and '}').

    In which case, it would be relatively easy to add syntax similar to:

    case [A-Z]:

    Which would, of course, be portable.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT

    Comment

    • Nate Eldredge

      #32
      Re: C/C++ language proposal: Change the 'case expression' from "integr al constant-expression&quot ; to "integr al expression&quot ;

      "robertwessel2@ yahoo.com" <robertwessel2@ yahoo.comwrites :
      On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
      >What will be next? "case >=7:"?
      >
      >
      Frankly I think ranges on the case constant expressions would be a
      more useful addition while staying with the basic philosophy of the C
      switch statement. IOW, "case 2...5:", or something along those
      lines. But still not something I'm loosing sleep over...
      GCC provides this as an extension, FWIW. You can write

      case 2 ... 5:

      The spaces are required to keep the parser from thinking it's some
      malformed floating point constant.

      Comment

      • Keith Thompson

        #33
        Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

        Willem <willem@stack.n lwrites:
        Keith Thompson wrote:
        ) Then programmers will inevitably write
        )
        ) case 'A' ... 'Z':
        )
        ) which is non-portable (under EBCDIC it matches '\' and '}').
        >
        In which case, it would be relatively easy to add syntax similar to:
        >
        case [A-Z]:
        >
        Which would, of course, be portable.
        It could be, if you defined it *very* carefully.

        Lexically, you have 7 tokens:
        case (a keyword)
        [ (a punctuator)
        A (an identifier)
        - (a punctuator)
        Z (an identifier)
        ] (a punctuator)
        : (a punctuator)

        Do you really intend the identifier A here to refer to the character
        'A', ignoring any declared entity called A? Do you really intend to
        specify a range of literal character values without using the "'"
        symbol? Which characters would be allowed? Does [A-Z] refer only to
        the 26 uppercase letters of the Latin alphabet? Could this notation
        be used for non-Latin letters? Digits? Punctuation symbols?

        Properly defined, it could make it easier to work with Latin letters
        -- which is both good and bad, since it would further encourage
        programmers to ignore the fact that other alphabets exist. Sometimes
        you really do want to determine whether a character matches one of the
        26 uppercase Latin letters, but more often what you *really* want is
        the locale-sensitive behavior of isupper().

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Willem

          #34
          Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

          Keith Thompson wrote:
          ) Willem <willem@stack.n lwrites:
          )In which case, it would be relatively easy to add syntax similar to:
          )>
          ) case [A-Z]:
          )>
          )Which would, of course, be portable.
          )
          ) It could be, if you defined it *very* carefully.
          )
          ) <snip>
          )
          ) Do you really intend the identifier A here to refer to the character
          ) 'A', ignoring any declared entity called A?

          Well, no. That's why I stated 'similar to'. It's just an idea after all.

          ) programmers to ignore the fact that other alphabets exist. Sometimes
          ) you really do want to determine whether a character matches one of the
          ) 26 uppercase Latin letters, but more often what you *really* want is
          ) the locale-sensitive behavior of isupper().

          Well, then how about

          case [:upper:]:

          Which, to be honest, is half-borrowed from Perl syntax.
          What the exact syntax is, isn't really relevant, I guess.


          SaSW, Willem
          --
          Disclaimer: I am in no way responsible for any of the statements
          made in the above text. For all I know I might be
          drugged or something..
          No I'm not paranoid. You all think I'm paranoid, don't you !
          #EOT

          Comment

          • Eric Sosman

            #35
            Re: C/C++ language proposal: Change the 'case expression' from &quot;integralc onstant-expression&quot ; to &quot;integr al expression&quot ;

            Willem wrote:
            [... case ranges for characters ...]
            Well, then how about
            >
            case [:upper:]:
            >
            Which, to be honest, is half-borrowed from Perl syntax.
            What the exact syntax is, isn't really relevant, I guess.
            The syntax is less important than getting the semantics
            right. One problem is in how to define the character classes;
            note that isupper() et al. are locale-dependent, so the set of
            characters that satisfy them is hard to compute at compile time.
            And there's still the issue of ambiguous branches:

            switch ('a') {
            case isalpha(ch): ...
            case islower(ch): ...
            case isxdigit(ch): ...
            case !ispunct(ch): ...

            "These arguments sound very well, but I can't help
            thinking that, if they were reduced to syllogistic form,
            they wouldn't hold water." -- R. Murgatroyd

            --
            Eric.Sosman@sun .com

            Comment

            • Eric Sosman

              #36
              Re: C/C++ language proposal: Change the 'case expression' from &quot;integralc onstant-expression&quot ; to &quot;integr al expression&quot ;

              Eric Sosman wrote:
              [...]
              And there's still the issue of ambiguous branches:
              >
              switch ('a') {
              case isalpha(ch): ...
              case islower(ch): ...
              case isxdigit(ch): ...
              case !ispunct(ch): ...
              Oh, botheration: I botched that one royally, didn't I? The
              fragment ought to have been something like

              ch = 'a';
              switch (0) {
              case !isalpha(ch): ...
              case !islower(ch): ...
              case !isxdigit(ch): ...
              case !!ispunct(ch): ...

              --
              Eric.Sosman@sun .com

              Comment

              • Hallvard B Furuseth

                #37
                Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

                Keith Thompson writes:
                >Frankly I think ranges on the case constant expressions would be a
                >more useful addition while staying with the basic philosophy of the C
                >switch statement. IOW, "case 2...5:", or something along those
                >lines. But still not something I'm loosing sleep over...
                >
                Then programmers will inevitably write
                >
                case 'A' ... 'Z':
                They do anyway. #define ISUPPER(c) ('A' <= (c) && (c) <= 'Z'). E.g. to
                check for the ASCII (or 7-bit Unicode) letters regardless of locale.
                which is non-portable (under EBCDIC it matches '\' and '}').
                --
                Hallvard

                Comment

                • Keith Thompson

                  #38
                  Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

                  Hallvard B Furuseth <h.b.furuseth@u sit.uio.nowrite s:
                  Keith Thompson writes:
                  >>Frankly I think ranges on the case constant expressions would be a
                  >>more useful addition while staying with the basic philosophy of the C
                  >>switch statement. IOW, "case 2...5:", or something along those
                  >>lines. But still not something I'm loosing sleep over...
                  >>
                  >Then programmers will inevitably write
                  >>
                  > case 'A' ... 'Z':
                  >
                  They do anyway. #define ISUPPER(c) ('A' <= (c) && (c) <= 'Z'). E.g. to
                  check for the ASCII (or 7-bit Unicode) letters regardless of locale.
                  >
                  >which is non-portable (under EBCDIC it matches '\' and '}').
                  Sure, but there's not much that can be done to discourage dumb macros.
                  Actually, there is: <ctype.halrea dy has the locale-sensitive
                  isupper() function.

                  The problem with the proposed "..." notation is that it makes it easy
                  to do the wrong thing (assuming that the uppercase letters have
                  contiguous codes and run from 'A' to 'Z') *without* making it any
                  easier to do the right thing (using isupper() to determine whether a
                  character is an uppercase letter).

                  On the other hand, there are certainly times when case ranges would be
                  handy for numeric ranges, as opposed to character ranges, and using
                  them wouldn't cause any problems. Other languages do provide similar
                  constructs. And I suppose compilers could warn about 'A' ... 'Z'.

                  Caveat: Sometimes ('A' <= c && c <= 'Z') *is* exactly what you want,
                  if you're writing deliberately non-portable code.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something. This is something. Therefore, we must do this."
                  -- Antony Jay and Jonathan Lynn, "Yes Minister"

                  Comment

                  • Sjouke Burry

                    #39
                    Re: C/C++ language proposal: Change the 'case expression' from &quot;integralc onstant-expression&quot ; to &quot;integr al expression&quot ;

                    robertwessel2@y ahoo.com wrote:
                    On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                    >What will be next? "case >=7:"?
                    >
                    >
                    Frankly I think ranges on the case constant expressions would be a
                    more useful addition while staying with the basic philosophy of the C
                    switch statement. IOW, "case 2...5:", or something along those
                    lines. But still not something I'm loosing sleep over...
                    That was already included in microsoft fortran 5.1 (extension)
                    in 1990 :) :)

                    Comment

                    • Keith Thompson

                      #40
                      Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

                      Sjouke Burry <burrynulnulfou r@ppllaanneett. nnlllwrites:
                      robertwessel2@y ahoo.com wrote:
                      >On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                      >>What will be next? "case >=7:"?
                      >Frankly I think ranges on the case constant expressions would be a
                      >more useful addition while staying with the basic philosophy of the C
                      >switch statement. IOW, "case 2...5:", or something along those
                      >lines. But still not something I'm loosing sleep over...
                      >
                      That was already included in microsoft fortran 5.1 (extension)
                      in 1990 :) :)
                      Pascal had it long before that, and it's likely that other languages
                      had it even earlier. There's certainly ample precedent. The question
                      is whether the benefits would outweigh the disadvantages, especially
                      considering the ambiguity of ranges like 'A' ... 'Z'.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • Hendrik Schober

                        #41
                        Re: C/C++ language proposal: Change the 'case expression' from &quot;integralc onstant-expression&quot ; to &quot;integr al expression&quot ;

                        Keith Thompson wrote:
                        "robertwessel2@ yahoo.com" <robertwessel2@ yahoo.comwrites :
                        >On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                        >>What will be next? "case >=7:"?
                        >Frankly I think ranges on the case constant expressions would be a
                        >more useful addition while staying with the basic philosophy of the C
                        >switch statement. IOW, "case 2...5:", or something along those
                        >lines. But still not something I'm loosing sleep over...
                        >
                        Then programmers will inevitably write
                        >
                        case 'A' ... 'Z':
                        >
                        which is non-portable (under EBCDIC it matches '\' and '}').
                        And why exactly would that be worse than an 'if'-'else' chain
                        relying on ASCII?

                        Schobi

                        Comment

                        • Pete Becker

                          #42
                          Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

                          On 2008-10-30 07:10:13 -0400, Hendrik Schober <spamtrap@gmx.d esaid:
                          Keith Thompson wrote:
                          >"robertwessel2 @yahoo.com" <robertwessel2@ yahoo.comwrites :
                          >>On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                          >>>What will be next? "case >=7:"?
                          >>Frankly I think ranges on the case constant expressions would be a
                          >>more useful addition while staying with the basic philosophy of the C
                          >>switch statement. IOW, "case 2...5:", or something along those
                          >>lines. But still not something I'm loosing sleep over...
                          >>
                          >Then programmers will inevitably write
                          >>
                          > case 'A' ... 'Z':
                          >>
                          >which is non-portable (under EBCDIC it matches '\' and '}').
                          >
                          And why exactly would that be worse than an 'if'-'else' chain
                          relying on ASCII?
                          >
                          An if-else chain in source code wouldn't rely on ASCII, but on the
                          source character set, which would, presumably, be EBCDIC when you're
                          targeting a machine that uses EBCDIC.

                          switch(ch)
                          {
                          case 'A':
                          case 'B':
                          case 'C':
                          case 'D':
                          ....
                          }

                          Each character will be properly encoded, even though there are extra
                          characters in the middle of the capital letters in EBCDIC.

                          --
                          Pete
                          Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
                          Standard C++ Library Extensions: a Tutorial and Reference
                          (www.petebecker.com/tr1book)

                          Comment

                          • Hendrik Schober

                            #43
                            Re: C/C++ language proposal: Change the 'case expression' from &quot;integralc onstant-expression&quot ; to &quot;integr al expression&quot ;

                            Pete Becker wrote:
                            On 2008-10-30 07:10:13 -0400, Hendrik Schober <spamtrap@gmx.d esaid:
                            >
                            >Keith Thompson wrote:
                            >>"robertwessel 2@yahoo.com" <robertwessel2@ yahoo.comwrites :
                            >>>On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                            >>>>What will be next? "case >=7:"?
                            >>>Frankly I think ranges on the case constant expressions would be a
                            >>>more useful addition while staying with the basic philosophy of the C
                            >>>switch statement. IOW, "case 2...5:", or something along those
                            >>>lines. But still not something I'm loosing sleep over...
                            >>Then programmers will inevitably write
                            >>>
                            >> case 'A' ... 'Z':
                            >>>
                            >>which is non-portable (under EBCDIC it matches '\' and '}').
                            > And why exactly would that be worse than an 'if'-'else' chain
                            > relying on ASCII?
                            >>
                            >
                            An if-else chain in source code wouldn't rely on ASCII [...]
                            I'd expect most of the programmers I worked with, in the
                            absent of the 'switch' syntax proposed above, to write
                            if( x>='A' && x<='Z' )
                            which is just as wrong.

                            So allowing this for #switch' IMO wouldn't make anything
                            worse.

                            Schobi

                            Comment

                            • Keith Thompson

                              #44
                              Re: C/C++ language proposal: Change the 'case expression' from &quot;integr al constant-expression&quot ; to &quot;integr al expression&quot ;

                              Hendrik Schober <spamtrap@gmx.d ewrites:
                              Keith Thompson wrote:
                              >"robertwessel2 @yahoo.com" <robertwessel2@ yahoo.comwrites :
                              >>On Oct 28, 7:42 pm, JoelKatz <dav...@webmast er.comwrote:
                              >>>What will be next? "case >=7:"?
                              >>Frankly I think ranges on the case constant expressions would be a
                              >>more useful addition while staying with the basic philosophy of the C
                              >>switch statement. IOW, "case 2...5:", or something along those
                              >>lines. But still not something I'm loosing sleep over...
                              >Then programmers will inevitably write
                              > case 'A' ... 'Z':
                              >which is non-portable (under EBCDIC it matches '\' and '}').
                              >
                              And why exactly would that be worse than an 'if'-'else' chain
                              relying on ASCII?
                              It wouldn't. The problem (as I think I've already said in this
                              thread) is that adding this syntax makes it much easier to check for
                              characters in the range 'A' to 'Z' *without* making it any easier to
                              check for characters that are uppercase letters.

                              It's not the least bit difficult to write bad code in C or C++ (this
                              is cross-posted), even with their current features, but let's not make
                              it even easier.

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              • Harald van =?UTF-8?b?RMSzaw==?=

                                #45
                                Re: C/C++ language proposal: Change the 'case expression' from&quot;integ ral constant-expression&quot ; to &quot;integr al expression&quot ;

                                On Wed, 29 Oct 2008 13:45:46 -0700, Keith Thompson wrote:
                                Caveat: Sometimes ('A' <= c && c <= 'Z') *is* exactly what you want, if
                                you're writing deliberately non-portable code.
                                It doesn't need to be *deliberately* non-portable. If you were
                                implementing your own C library, on an ASCII-based machine with minimal
                                locale support, this could be the best way to write isupper.

                                Comment

                                Working...