🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3621

Re: Switch switch

#3621
From: Jim Fleming
Date: Thu, 03 Aug 1995 02:20
124 lines
5250 bytes
In article <3vno05$il7@kruuna.helsinki.fi>, wirzeniu@cc.Helsinki.FI says...
>
>jim.fleming@bytes.com (Jim Fleming):
>> Does anyone want to be able to write....???
>> 
>>       switch (aString) {
>>               ...
>>       case "hello":
>
>Would this be equivalent to
>
>        aString == "hello"      (compare pointers)
>        strcmp(aString, "hello") == 0   (compare strings)
>        strcasecmp(aString, "hello") == 0       (case in-sensitive 
comparison)
>
>or something else?
>
@@@@@@@@@@@@@@@@@@@

For the simple cases, the compiler should be "smart enough" to figure
out what is going on...unfortunately, we would have to precisely define
what happens when you mix types in the case statements. This could be
outlawed, but some programmers may prefer to use an integer instead of
a string with an integer. The compiler could take the type of the
expression in the "switch (<expression>)" and optimize the switch as
long as the semantics are preserved. In some cases, the compiler might
note that only the first two, three or four characters must be compared
for uniqueness and a fast 32 bit comparison could be done...


@@@@@@@@@@@@@@@@@@@

>If it is anything but the first one, the potential for hidden costs
>is fairly high.  Also, a mere strcmp is not versatile enough to be 
>worth it, you really need to be able to do strcasecmp (yes, I know
>it isn't a standard function), strncmp, strncasecmp, strnumcmp, 
>memcmp, ....
>
@@@@@@@@@@@@@@@@@@@

Yes, the costs can be high, but compiler developers could distinguish
themselves based on their clever techniques...

@@@@@@@@@@@@@@@@@@@

>If you want to make switch more versatile, do it properly and allow
>arbitrary conditions:
>
>        caselist {
>        case strcmp(aString, "hello") == 0:
>        case strcmp(aString, "hi there") == 0:
>                printf("hello, sailor\n");
>                break;
>        case rand() < RAND_MAX/2:
>                printf("whaddaya want?\n");
>                break;
>        default:
>                printf("how may I serve you, sir?\n");
>                break;
>        }
>
>The conditions would be evaluated one by one, top to bottom, until
>one is satisfied.  The optional "default" would be evaluated only
>if no other condition would be true.
>
>Of course, this is just syntactic sugar for a few nested if-else's,
>but in some cases it would be much clearer.
>
@@@@@@@@@@@@@@@@@

Yes...this brings us to the point of "what is a switch statement?"...
	Is it a clear way to write what can also be done with
	if statements?

In my opinion, there is something fundamental in having one expression
evaluated and then compared to a variety of choices. If those choices
are expressions which must be evaluated, then we have to precisely
specify the order of execution.

There is merit in considering a parallel expression evaluation for
all of the case statement expressions. This gets dangerous because
the expressions could use results from the switch expression or from
the other case statements.

@@@@@@@@@@@@@@@@@

>But I'm not advocating this, since I'd prefer to see C stay as
>unchanging as possible.  It's now a nice, almost universally
>portable language (not counting a few systems that _still_ haven't
>got a c89 compiler or libraries).  Any new standard has a high
>probability of destroying this, again.
>
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Yes, I would like the syntax of the language to remain the same. In some
cases, semantic extensions can be added by relaxing restrictions
imposed back in the 70's. This might make it easier for programmers
to begin think toward next generation, dynamically bound object-oriented
languages, without giving up C...

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

>(I'm not agains minor additions, such as // comments or enhancements
>to the library, but I think the language part should stay the same
>as much as possible.)
>
>-- 
>Lars.Wirzenius@helsinki.fi  (finger wirzeniu@klaava.helsinki.fi)
>Publib version 0.5: ftp://ftp.cs.helsinki.fi/pub/Software/Local/Publib/
@@@@@@@

Yes, I would like C to remain C...
	as we say here in the states...from C to shining C...

-- 
Jim Fleming            /|\      Unir Corporation       Unir Technology, Inc.
jrf@tiger.bytes.com  /  | \     One Naperville Plaza   184 Shuman Blvd. #100
%Techno Cat I       /   |  \    Naperville, IL 60563   Naperville, IL 60563
East End, Tortola  |____|___\   1-708-505-5801         1-800-222-UNIR(8647)
British Virgin Islands__|______ 1-708-305-3277 (FAX)   1-708-305-0600
                 \__/-------\__/       http:199.3.34.13 telnet: port 5555
Smooth Sailing on Cruising C+@amarans  ftp: 199.3.34.12 <-----stargate----+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\____to the end of the OuterNet_|
Technical Details
Message-ID: <3vpbpo$nl6@News1.mcs.net>
Path: arc1-ca.pugleaf.net!news.mcs.net!usenet
References: <3vn92m$oqt@news1.mcs.net> <3vno05$il7@kruuna.helsinki.fi>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mcs.net!usenet
From: jim.fleming@bytes.com (Jim Fleming)
Newsgroups: comp.std.c
Subject: Re: Switch switch
Date: 3 Aug 1995 02:20:40 GMT
Organization: Unir Corporation
Message-ID: <3vpbpo$nl6@News1.mcs.net>
References: <3vn92m$oqt@news1.mcs.net> <3vno05$il7@kruuna.helsinki.fi>
NNTP-Posting-Host: pc11.bytes.com
Mime-Version: 1.0
X-Newsreader: WinVN 0.93.14