August 29, 2002
"Joel Lucsy" <jjlucsy@ameritech.net> wrote in message news:Xns9277E3774C7F3jjlucsy@63.105.9.61...
> "Richard Krehbiel" <rich@kastle.com> wrote in news:akg8a1$2j04$1@digitaldaemon.com:
>
> > My personal belief is:
> >
> > 1. Case enumerations ("case 1, 2, 3:") and ranges ("case 1..5:")
> > should be supported; and
> > 2. Fallthrough behavior should be eliminated; all cases have an
> > implicit break.
> >
> > In the last tiny few percent of cases where a deliberate fallthrough is needed, you can use a plain old goto.
>
> How about this in addition to:
> switch (something)
> {
>     case 0:
>         /*dosomething and fallthru to case 1*/
>     nobreak case 1:
>         /*dosomething else*/
>     case 2:
>         /*dosomething entirely else, case 1 won't get here*/
> }
>

I'm sorry I don't see much of an advantage here. Mite as well add a brake in this case. It's not much better.


August 29, 2002
I apologise if this idea has already been mentioned. I'd like some form of non-case fall though parhaps like this:

switch (i)
{
    case (1, 2, 3) //Auto non fall though
    case 6: //Normal
    default: //Normal
}

Neat, and it stands out.

Futher more the name could be changed, if there's still problems with the above.

switch (i)
{
    select (1, 2, 3) //Auto non fall though
    case 6: //Normal
    default: //Normal
}


August 29, 2002
anderson wrote:

> switch (i)
> {
>     select (1, 2, 3) //Auto non fall though
>     case 6: //Normal
>     default: //Normal
> }

That seems a good idea.  However "select" may be better replaced by "when" as in Ada (less typeing).

If a new keyword is used then the brackets would not really be needed, though it would make little difference, to me, wether they were included or not.

mandatory example...

switch(i) {
        when(1,2,3) {   /* auto break - recommended */ }
        case 6:         /* standard C */
        default:                /* standard C */
}

or

switch(i) {
        when 1,2,3:     /* auto break - recommended */
        case 6:         /* standard C */
        default:                /* standard C */
}

C 2002/8/29
August 29, 2002
Fallthrough is a sometimes-important feature for expert C programmers.  I have used it on rare occasions.  A new keyword "fallthrough" would be good.  Better, "goto case N" permitting arbitrary jumps to any other part of the case statement.

The real problem with C switch statements is the const requirement on the tests. C switch statements will not compile unless all tests resolve to integer comparisons at compile time.  This is silly because the alternative (if/else if/else) technique works fine, and there is no reason switch should not cover the same ground.  It is just a C legacy problem.  Maybe D can fix it.

I recently desired to construct a switch based on a std::string objects.  The ideal compiler should be smart enough to create const std::string objects from the constant strings, and also to apply std::string::operator==() for the case tests.  Perhaps D could pull of what C++ never did.

Mark


std::string    variable;
switch (variable)
{
case "a_certain_string_1":
// do stuff
break;
case "a_certain_string_2":
// do other stuff
break;
default:
break;
}


August 30, 2002
Yes, less typing is good. I don't like mandatory bracketing {} though. I think things get more complex when you provide to many ways of doing them. If your going to use when, then () aren't needed I suppose. I think we need to agree on one standard form.

Users of D would then be encourage to use "when". "case" would be used in the rare occasions (like gotos are).

"C.R.Chafer" <blackmarlin@nospam.asean-mail.com> wrote in message news:akl2n5$39d$1@digitaldaemon.com...
> anderson wrote:
>
> > switch (i)
> > {
> >     select (1, 2, 3) //Auto non fall though
> >     case 6: //Normal
> >     default: //Normal
> > }
>
> That seems a good idea.  However "select" may be better replaced by "when"
> as in Ada (less typeing).
>
> If a new keyword is used then the brackets would not really be needed, though it would make little difference, to me, wether they were included
or
> not.
>
> mandatory example...
>
> switch(i) {
>         when(1,2,3) {   /* auto break - recommended */ }
>         case 6:         /* standard C */
>         default:                /* standard C */
> }
>
> or
>
> switch(i) {
>         when 1,2,3:     /* auto break - recommended */
>         case 6:         /* standard C */
>         default:                /* standard C */
> }
>
> C 2002/8/29


August 30, 2002
Mark Evans <Mark_member@pathlink.com> wrote in news:akm8e0$1dj3$1@digitaldaemon.com:

> I recently desired to construct a switch based on a std::string objects.  The ideal compiler should be smart enough to create const std::string objects from the constant strings, and also to apply std::string::operator==() for the case tests.  Perhaps D could pull of what C++ never did.

D also requires cases to be constant, but as a special case it allows to use char[] and wchar[] in switch:

    	switch (args[1])
    	{
    	case "-h":
    	case "-?":
    	    	...
    	case "-x":
    	    	...
    	}
August 30, 2002
Just to sum up where we are so far...
(my comments are in square brackets []) (please refer to ajoined
thread for original contributers of various ideas)

(0)     something needs to be done about errors caused by
        default fall though behaviour in switch statements
(1)     switch statement should be compatible with C switch statement
(1a)    case should always be fall through [backward compatibility]
(1b)    fallthough statement just adds extra typeing resulting in little
        gain for experienced users. [seems people do not like this idea]
(1c)    case <x> {} is easily mixed up with case <x>: {} and therefore
        that syntax is no good. [idea withdrawn]
(2)     a new keyword ['when' seems good - here after refered to as
        when? to signify the actual keyword has not been decided]
        should be added which exibits default break behaviour
(2a)    'case' should be not recommended (but present where
        fallthough is needed - and for porting C apps) similar to goto.
(2b)    when? should be recommended.
(2c)    when? format syntax should be easy to add to the current
        compiler with out to much effort [though having not seen the
        compiler internals this is only a guess].
(3)     enhanced case & when? statements
(3a)    support for other constant types (D supports strings currently)
(3b)    multiple options (case 1,5,9:)
(3c)    ranged options (case 1..6:)
(3d)    variable options (case myVar:)  [I would not recommend
        this as I see a switch statement as a hint to the compiler that
        a lookup table may be a good optimisation in this situation -
        variable options prevent such a table being constructed and
        the 'else if' construct is often suitable for this case]

Not discussed / or discussed in other threads.
(4)     automatic 'default: assert;' where default is ommited
        [seems a good idea - I think D already does this].
(5)     break; and continue; should continue to operate in same way
        as in C - as break aLabel; and continue aLabel; can be used
        when different behaviour is required - an should probably be
        recommended as they are far clearer.
(5a)    aLbl: switch(..){ .. continue aLbl; .. } could be useful for
        implementing state machines.
(5b)    continue; and break; behaviour slightly confusing in switch
        statements.

Comments please (especially from Walter - who has as yet not contributed to this thread).

C 2002/8/30
August 30, 2002
Something you guys have missed is that if you leave the existing behavior for case:, you will also end up leaving the existing behavior for default:. Adding a new when keyword won't fix everything.

I don't know which posts you've been reading but it seems that an awful lot of the contributors don't want C compatibility.  C interoperability is nice. But if I wanted to program in C, I'd program in C.

I suppose goto case would be better than fallthrough.

Sean

"C.R.Chafer" <blackmarlin@nospam.asean-mail.com> wrote in message news:akntei$4up$1@digitaldaemon.com...
> Just to sum up where we are so far...
> (my comments are in square brackets []) (please refer to ajoined
> thread for original contributers of various ideas)
>
> (0)     something needs to be done about errors caused by
>         default fall though behaviour in switch statements
> (1)     switch statement should be compatible with C switch statement
> (1a)    case should always be fall through [backward compatibility]
> (1b)    fallthough statement just adds extra typeing resulting in little
>         gain for experienced users. [seems people do not like this idea]
> (1c)    case <x> {} is easily mixed up with case <x>: {} and therefore
>         that syntax is no good. [idea withdrawn]
> (2)     a new keyword ['when' seems good - here after refered to as
>         when? to signify the actual keyword has not been decided]
>         should be added which exibits default break behaviour
> (2a)    'case' should be not recommended (but present where
>         fallthough is needed - and for porting C apps) similar to goto.
> (2b)    when? should be recommended.
> (2c)    when? format syntax should be easy to add to the current
>         compiler with out to much effort [though having not seen the
>         compiler internals this is only a guess].
> (3)     enhanced case & when? statements
> (3a)    support for other constant types (D supports strings currently)
> (3b)    multiple options (case 1,5,9:)
> (3c)    ranged options (case 1..6:)
> (3d)    variable options (case myVar:)  [I would not recommend
>         this as I see a switch statement as a hint to the compiler that
>         a lookup table may be a good optimisation in this situation -
>         variable options prevent such a table being constructed and
>         the 'else if' construct is often suitable for this case]
>
> Not discussed / or discussed in other threads.
> (4)     automatic 'default: assert;' where default is ommited
>         [seems a good idea - I think D already does this].
> (5)     break; and continue; should continue to operate in same way
>         as in C - as break aLabel; and continue aLabel; can be used
>         when different behaviour is required - an should probably be
>         recommended as they are far clearer.
> (5a)    aLbl: switch(..){ .. continue aLbl; .. } could be useful for
>         implementing state machines.
> (5b)    continue; and break; behaviour slightly confusing in switch
>         statements.
>
> Comments please (especially from Walter - who has as yet not contributed
to
> this thread).
>
> C 2002/8/30


August 31, 2002
Default should be recomended to be last, therefore there should be no problem.

switch
{
    when 1,2,3:
        ...
    case 4:
        ...
    break;
    default:
        ...
    break; //put it in or out, it doesn't matter.
}

Would be be re-interperated by the compiler as:

switch
{
    case 1:
    case 2:
    case 3:
        ...
    break;
    case 4:
        ...
    break;
    default:
        ...
}

Note that a break could still be used in a when statement, such as in an if statement for early exit.

switch
{
    when 1,2,3:
        if (error) break;
        ...
    case 4:
        ...
    break;
    default:
        ...
    break; //put it in or out, it doesn't matter.
}

Simply using a differn't word, and making things backwards compatable, will reduce confusion.

It's also be nice if when supported ranges.

switch
{
    when 1..10:
    break;
    default:
};

"Sean L. Palmer" <seanpalmer@earthlink.net> wrote in message news:ako6oc$fb7$1@digitaldaemon.com...
> Something you guys have missed is that if you leave the existing behavior for case:, you will also end up leaving the existing behavior for
default:.
> Adding a new when keyword won't fix everything.
>
> I don't know which posts you've been reading but it seems that an awful
lot
> of the contributors don't want C compatibility.  C interoperability is
nice.
> But if I wanted to program in C, I'd program in C.
>
> I suppose goto case would be better than fallthrough.
>
> Sean
>
> "C.R.Chafer" <blackmarlin@nospam.asean-mail.com> wrote in message news:akntei$4up$1@digitaldaemon.com...
> > Just to sum up where we are so far...
> > (my comments are in square brackets []) (please refer to ajoined
> > thread for original contributers of various ideas)
> >
> > (0)     something needs to be done about errors caused by
> >         default fall though behaviour in switch statements
> > (1)     switch statement should be compatible with C switch statement
> > (1a)    case should always be fall through [backward compatibility]
> > (1b)    fallthough statement just adds extra typeing resulting in little
> >         gain for experienced users. [seems people do not like this idea]
> > (1c)    case <x> {} is easily mixed up with case <x>: {} and therefore
> >         that syntax is no good. [idea withdrawn]
> > (2)     a new keyword ['when' seems good - here after refered to as
> >         when? to signify the actual keyword has not been decided]
> >         should be added which exibits default break behaviour
> > (2a)    'case' should be not recommended (but present where
> >         fallthough is needed - and for porting C apps) similar to goto.
> > (2b)    when? should be recommended.
> > (2c)    when? format syntax should be easy to add to the current
> >         compiler with out to much effort [though having not seen the
> >         compiler internals this is only a guess].
> > (3)     enhanced case & when? statements
> > (3a)    support for other constant types (D supports strings currently)
> > (3b)    multiple options (case 1,5,9:)
> > (3c)    ranged options (case 1..6:)
> > (3d)    variable options (case myVar:)  [I would not recommend
> >         this as I see a switch statement as a hint to the compiler that
> >         a lookup table may be a good optimisation in this situation -
> >         variable options prevent such a table being constructed and
> >         the 'else if' construct is often suitable for this case]
> >
> > Not discussed / or discussed in other threads.
> > (4)     automatic 'default: assert;' where default is ommited
> >         [seems a good idea - I think D already does this].
> > (5)     break; and continue; should continue to operate in same way
> >         as in C - as break aLabel; and continue aLabel; can be used
> >         when different behaviour is required - an should probably be
> >         recommended as they are far clearer.
> > (5a)    aLbl: switch(..){ .. continue aLbl; .. } could be useful for
> >         implementing state machines.
> > (5b)    continue; and break; behaviour slightly confusing in switch
> >         statements.
> >
> > Comments please (especially from Walter - who has as yet not contributed
> to
> > this thread).
> >
> > C 2002/8/30
>
>


August 31, 2002
anderson wrote:

> Default should be recomended to be last, therefore there should be no problem.
> 
> switch
> {
>     when 1,2,3:
>         ...
>     case 4:
>         ...
>     break;
>     default:
>         ...
>     break; //put it in or out, it doesn't matter.
> }
> 
> Would be be re-interperated by the compiler as:
> 
> switch
> {
>     case 1:
>     case 2:
>     case 3:
>         ...
>     break;
>     case 4:
>         ...
>     break;
>     default:
>         ...
> }
> 
> Note that a break could still be used in a when statement, such as in an if statement for early exit.
> 
> switch
> {
>     when 1,2,3:
>         if (error) break;
>         ...
>     case 4:
>         ...
>     break;
>     default:
>         ...
>     break; //put it in or out, it doesn't matter.
> }

That was exactly what I thought.

> Simply using a differn't word, and making things backwards compatable, will reduce confusion.
> 
> It's also be nice if when supported ranges.
> 
> switch
> {
>     when 1..10:
>     break;
>     default:
> };

Yes, nice, though not required at the present time.
However, the 'when' statement (in my opinion)
should be included as soon as possible as it should
solve one of the remaining potential bug causes in D.

C 2002/8/31
1 2
Next ›   Last »