[Issue 4349] Deprecate automatic case fallthrough

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 21 03:17:35 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4349



--- Comment #1 from bearophile_hugs at eml.cc 2010-06-21 03:17:29 PDT ---
Sean Kelly and Michel Fortin have shown some code examples.

This D code:

switch (x) {
    case 1: case 2: case 3: case 4:
        doSomething();
        break;
    default:
        whatever();
}


With the new proposal can be translated like this (this is already D syntax):

switch (x) {
    case 1, 2, 3, 4:
        doSomething();
        break;
    default:
        whatever();
        break;
}


C/C++ programmers that will want to use D will have to learn the new rule that
says: "In D there is no automatic case fallthrough". But allowing the
fallthrough when there are no statements after the label is a special case to
the general rule that they have to learn: ", unless there are no statements
inside the case.".

Special cases are bad in a programming language, they make manuals longer,
require more time and energy for the programmer to learn them, make the
compiler more complex and more buggy, and often slow down the programming even
whey they were created to give a handy special case.

-----------------

This D code gives a little more problems:

switch (x) {
    case 1: .. case 10:
    case 22: .. case 32:
    case 52, 64:
        doSomething();
        break;
    default:
        whatever();
}


With the new proposal it can be translated like this (this is new syntax), that
considers a closed range of values one of the possible items of the list of
cases:

switch (x) {
    case 1: .. case 10,
    case 22: .. case 32,
    52, 64:
        doSomething();
        break;
    default:
        whatever();
}


Or just like this (this is normal D syntax):

switch (x) {
    case 1: .. case 10: goto case;
    case 22: .. case 32: goto case;
    case 52, 64:
        doSomething();
        break;
    default:
        whatever();
}


This shorter syntax (that uses a single 'case' statement) is now not available:

switch (x) {
    case 1 ... 10, 22 ... 32, 52, 64:
        doSomething();
        break;
    default:
        whatever();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list