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

Kevin Ballard kevin at sb.org
Fri Dec 4 16:20:28 CST 2015

I don't use `fallthrough` very often, but when I do, it's extremely
useful. Removing it would make some of my code significantly harder to
read. In fact, I actually wish `fallthrough` was more powerful, allowing
me to fall into cases that bound identifiers if every bound identifier
was already bound with the same type in the current case, though I
understand why the language doesn't allow it (it's significant
complexity for a relatively minor gain).

Similarly, the ability to use `break` in switch statements, while not
something I use very often outside of using it for empty cases, is still
invaluable when I do occasionally need it. And since you can already
break out of loops by using a label, I don't see why we should remove
it. An example of doing that is

loop: for thing in collection {
    switch thing {
    case .Special: break loop
    default: ...

-Kevin Ballard

On Fri, Dec 4, 2015, at 02:14 PM, Sean Heber wrote:
> I’ve always shied away from fallthrough no matter what language I’m
> using. Perhaps there are situations where it is required, but in spirit I
> agree it maybe should be removed.
> I’d amend this to also prevent “break” from applying to switch statements
> so that they can break out of loops if you’ve nested a switch inside of a
> loop. I ran into this situation and spent entirely too long confused
> about why it didn’t break out like I thought it should have:
> for thing in collection {
>   switch thing {
>   case .Special: break
>   default { … }
>   }
> }
> The reason I expected this to work is that I don’t need to use a “break”
> in Swift cases so it stood to reason this would mean the “break”
> statement would only apply to the loop. I was wrong. :P
> l8r
> Sean
> > On Dec 4, 2015, at 4: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?
> > 
> > My understanding is this keyword is only used for the archaic seeming purpose of perpetuating C-style fallthrough from one switch statement to the subsequent one. The documentation hedges the use of this keyword in forbidding terms that make it clear its use is not encouraged. The presence of the keyword, while an improvement over C’s implicit fallthrough, is a mark of inelegance on an otherwise well-designed, opinionated implementation of swtich statements.
> > 
> > The ugliness of fallthrough’s C-style behavior even demands a caveat in the documentation:
> > 
> > "The fallthrough keyword does not check the case conditions for the switch case that it causes execution to fall into. The fallthrough keyword simply causes code execution to move directly to the statements inside the next case (or default case) block, as in C’s standard switch statement behavior."
> > 
> > To my mind, the caveat explains just what is wrong with fallthrough, both in C or Swift: coded that is clearly labeled with deliberate conditions can nonetheless be reached.
> > 
> > I quipped about this on Twitter, and the most common pushback I got seemed to be from people who either did not know about Swift’s support for comma-separated case statements, or harbored an aesthetic preference for clustering such cases together with fallthrough statements.
> > 
> > In my opinion, unless somebody can think of a strong defense for supporting intentional fallthrough in Swift, removing the keyword would be a move in the direction of minimizing the language’s complexity while also discouraging poor coding style in switch statements.
> > 
> > Thoughts?
> > 
> > Daniel
> > 
> > _______________________________________________
> > swift-evolution mailing list
> > swift-evolution at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-evolution
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

More information about the swift-evolution mailing list