Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"

Reply
Thread Tools

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

 
 
Mark L Pappin
Guest
Posts: n/a
 
      10-28-2008
Keith Thompson <(E-Mail Removed)> writes:

> And of course it would break every existing program that uses "cond"
> as a keyword,


ITYM "as an identifier,".

mlp
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-28-2008
Mark L Pappin <(E-Mail Removed)> writes:
> Keith Thompson <(E-Mail Removed)> writes:
>> And of course it would break every existing program that uses "cond"
>> as a keyword,

>
> ITYM "as an identifier,".


D'oh! Yes, thanks.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
 
 
 
JoelKatz
Guest
Posts: n/a
 
      10-29-2008
On Oct 25, 10:43*am, Jeff Schwab <(E-Mail Removed)> wrote:

> In what way would your altered switch be superior to an if/else tree?


Two ways:

1) An if/else tree hides the fact that the same variable is being
tested in each branch. A switch/case makes it clear that the same
expression is being compared in each case.

2) In some cases, it would make truly atrocious code look better. For
example, consider cases where you need to fall through from one case
to another or where you would otherwise need a temporary to avoid
multiple evaluations of the switch variable.

> The primary reason to use a switch is that it may be translated at
> compile-time into a jump table, but that benefit goes away if the
> statements are not known until run-time. *Languages like Ruby have
> switches with run-time case expressions because they delay the
> translation until run-time, anyway; such languages are using the syntax
> to mean something very different from what the C switch means. *IOW,
> languages that provide case statements of the form you've suggested do
> so only because they *can't* do what C and C++ do.


But they don't provide what he's asking for, and it's a natural
extension of the semantics of the switch/case statement. We could have
a special 'if' that allowed the implementation to cycle a specified
constant, either by counting up, down, or sideways if that was most
efficient. But we don't. We expect the optimizer to figure out how our
code can be micro-optimized.

Why do we have do/while, while, and for? You can make any code ugly by
choosing the wrong one, but you can make a lot of code nice by
choosing the best one.

The biggest argument against this proposal is that it's not really all
that useful. There really just aren't that many places where you could
use something like this.

In a 500,000 C++ line project I'm very familiar with, there are three
places where this could actually be useful. For comparison, it has 818
switch statements.

In two of those cases, it cleans up some ugliness where you need an if/
else tree before a switch/case statement.

What will be next? "case >=7:"?

DS
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      10-29-2008
On Oct 28, 7:24 pm, Matthias Buelow <(E-Mail Removed)> wrote:
> Adem wrote:
> > C/C++ language proposal:
> > Change the 'case expression' from "integral
> > constant-expression" to "integral expression"


> How about:


> cond {
> (foo == bar): ...; break;
> (!bar && (baz == boo)): ...; /* fallthru */
> default: ...; break;
> }


> In pseudo-grammar:


> cond { [<expr>|default: <stmt>*]* }


I once designed a language with something like that, many, many
years back. In fact, the grammar was a bit more complete,
something like:

cond_stmt := 'cond' [<expr1>] '{' case_list '}'
case_list := /* empty */ | case_list case_clause
case_clause := 'case' [<op>] expr2 ':' stmt
| 'default' ':' stmt

If expr1 was absent, <op> was forbidden, and it worked exactly
like your suggestion (except that there was no fall through---a
case controlled exactly one statement). If expr1 was present,
it was the equivalent of having written "case <expr1> <op>
<expr2>" for each case, except that expr1 was only evaluated
once; if you omitted the <op> in this case, it defaulted to ==,
so you could write things like:

cond x {
case > 0 : ... ;
case == 0 : ... ;
case < 0 : ... ;
}

or

cond c {
case 'a' : ... ;
case 'b' : ... ;
case 'c' : ... ;
}

(IIRC, the keyword was actually select, and not cond, and I used
OF .. END instead of {..}. But the basic idea was the same.)

The idea was basically that there are only four basic structured
constructs: a loop, a choice, a sequence, and a procedure call,
and thus, there were only four basic execution statements.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      10-29-2008
On Oct 28, 7:42*pm, JoelKatz <(E-Mail Removed)> wrote:
> 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...
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:
> On Oct 28, 7:42*pm, JoelKatz <(E-Mail Removed)> wrote:
>> 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 '}').

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      10-29-2008
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
 
Reply With Quote
 
Nate Eldredge
Guest
Posts: n/a
 
      10-29-2008
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> On Oct 28, 7:42*pm, JoelKatz <(E-Mail Removed)> wrote:
>> 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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2008
Willem <(E-Mail Removed)> writes:
> 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_Keith) (E-Mail Removed) <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"
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      10-29-2008
Keith Thompson wrote:
) Willem <(E-Mail Removed)> writes:
)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Change your language and you change your thoughts." Suganya C Programming 0 04-29-2008 01:35 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
A language-agnostic language Ed Java 24 03-27-2006 08:19 PM
Using a Scripting Language as Your Scripting Language DaveInSidney Python 0 05-09-2005 03:13 AM
Python is the best and most popular general purpose scripting language; the universal scripting language Ron Stephens Python 23 04-12-2004 05:32 PM



Advertisments