[swift-evolution] Proposal: Remove the "fallthrough" keyword

Pierre Habouzit phabouzit at apple.com
Sat Dec 5 11:15:26 CST 2015


> On Dec 5, 2015, at 9:02 AM, Chris Lattner <clattner at apple.com> wrote:
> 
> On Dec 4, 2015, at 2:05 PM, jalkut at red-sweater.com wrote:
>> In the spirit of some other proposals that remove C or C++ style artifacts, what do folks think about the possibility of removing the "fallthrough" keyword from the language?
> 
> I’m not making an argument either way, but I want to point something out: there is a major difference between fallthrough vs ++/c-style-for.  To someone who doesn’t know them, the later are "syntactic magic” with no reasonable way to decipher other than looking them up somewhere.  The former is an English word whose meaning is obvious in context.
> 
> All I’m saying is that to a reader of code, the “badness” of ++ and c-style for loops is greater than the “badness" of fallthrough.

Given that Swift has the goal to also be a low level language, fallthrough is really useful for conciseness and readability.

in system programming C, I find myself writing things like this very often:


switch (some_value) {
case ENUM_VALUE_REFINED:
    if (validate(some_value)) {
        return NULL;
    }
    /* fallthrough */
case ENUM_VALUE_BASE:
    handle_enum_value();
    …
}

Where the swift equivalent would roughly be:

switch some_value {
case .REFINED:
    if !validate(some_value) { return NULL }
    fallthrough
case .BASE:
    handle_enum_value();
}

This is as readable as it gets and is a pattern that the libdispatch has in several places e.g.

Of course, you cannot fall through to arbitrary cases, so things like this C code cannot be done in swift:

switch (some_value) {
case ENUM_VALUE_REFINED_1:
    if (validate(some_value)) {
        return NULL;
    }
    goto base_value;
case ENUM_VALUE_REFINED_2:
    if (validate(some_value)) {
        return NULL;
    }
    goto base_value;

case ENUM_VALUE_BASE:
base_value:
    handle_enum_value();
    …
}


cannot be written in swift, despite also being quite useful.

Jumping between arbitrary points inside a switch is disgusting. jumping from label to label is useful and not harmful especially in swift where you can’t place code between the “switch” and the first case.

-Pierre
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/0198fd04/attachment-0001.html>


More information about the swift-evolution mailing list