[swift-evolution] [Pitch] Introduce continue to switch statements

Xiaodi Wu xiaodi.wu at gmail.com
Sun Jul 10 23:16:59 CDT 2016

On Sun, Jul 10, 2016 at 10:48 PM, Erica Sadun <erica at ericasadun.com> wrote:

> > On Jul 10, 2016, at 8:37 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> >
> > This is a neat idea, and I think a very sensible way to extend the
> language. I worry only a little about the following:
> >
> > Currently, unless preceded immediately by the keyword `fallthrough`, a
> condition implicitly excludes all previous conditions. That is, if I write
> `switch .. { case a: ...; case b: ...; case c: ... }`, my condition `c` is
> really `!a && !b && c`. With more flexible control flow within a switch
> statement, reasoning about what cases are matched by any particular
> condition after the first becomes increasingly difficult.
> In the current Swift, absent `fallthrough`, the statement execution ends
> and no other statements are evaluated after the first match.
> With `fallthrough` the current clause executes and the next clause
> executes, and then the statement execution ends.
> With `continue`, the current clause executes and the switch continues
> searching for a matching pattern as if a pattern has not yet been matched.

I understand. What I'm saying is that control flow becomes more difficult
to reason through in a scenario as follows, which is not possible currently:

Given patterns A, B, C, and D, suppose a value x matches A, C, and D,
whereas another value y matches B and D, and a third value matches B and
C. When evaluating x, y, or z, which statements are executed in the
following switch statement? How many of these reach the default case? What
happens if I append `fallthrough` at the end of case D? What happens if I
move case B after case D? (Yes, I know it is possible to figure it out [my
understanding of the answer to the first question is appended below], but I
hope you'll agree with me that this is much more difficult to decipher than
any switch statement that's currently possible.)

switch x /* or y, or z */ {
case A:
  // ...
case B:
  // ...
  if C ~= x /* or y, or z, whichever is switched over */ {
case C:
  // ...
  if B ~= x /* or y, or z, whichever is switched over */ {
case D:
  // ...
  // ...

[For switch x: statements in cases A and C only, not the default; for
switch y: statements in case B, C and D, not the default; for switch z:
statements in cases B and C, and in the default]

Finally, a really neat thing about `continue` in Swift is the ability to
use labels; would you propose allowing that here? If so, could I label
individual cases and have pattern matching resume at that case? How about
resuming at a previous case? It'd be neat. It'd also make it possible to
make an infinite loop out of a switch statement...

-- E
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160710/de4140bd/attachment.html>

More information about the swift-evolution mailing list